Extracting information from a scanned GS1-type barcode

前端 未结 3 1655
傲寒
傲寒 2020-11-30 12:29

I am developing an app on Android Studio to get the expiry date of items upon scanning the barcode.

I don\'t want the user to install ZXing barcode app separately

3条回答
  •  伪装坚强ぢ
    2020-11-30 12:43

    This is a solution written in Javascript proven in a specific customer, generalization requires more work:

    //define AI's, parameter name and, optionally, transformation functions
    SapApplicationIdentifiers= [
        { ai: '00', regex: /^00(\d{18})/,         parameter: 'SSCC'},
        { ai: '01', regex: /^01(\d{14})/,         parameter: 'EAN'},
        { ai: '02', regex: /^02(\d{14})/,         parameter: 'EAN'},
        { ai: '10', regex: /^10([^\u001D]{1,20})/,  parameter: 'LOTE'},
        { ai: '13', regex: /^13(\d{6})/},
        { ai: '15', regex: /^15(\d{6})/,        parameter: 'F_CONS', transform: function(match){ return '20'+match[1].substr(0,2)+'-'+match[1].substr(2,2)+'-'+match[1].substr(4,2);}},
        { ai: '17', regex: /^17(\d{6})/,        parameter: 'F_CONS', transform: function(match){ return '20'+match[1].substr(0,2)+'-'+match[1].substr(2,2)+'-'+match[1].substr(4,2);}},
        { ai: '19', regex: /^19(\d{6})/,        parameter: 'F_CONS', transform: function(match){ return '20'+match[1].substr(0,2)+'-'+match[1].substr(2,2)+'-'+match[1].substr(4,2);}},
        { ai: '21', regex: /^21([\d\w]{1,20})/},                       //numero de serie
        { ai: '30', regex: /^30(\d{1,8})/},
        { ai: '310', regex: /^310(\d)(\d{6})/, parameter: 'NTGEW', transform: function(match){ return parseInt( match[2] ) / Math.pow( 10,parseInt( match[1] ) )}},
        { ai: '320', regex: /^320(\d)(\d{6})/, parameter: 'NTGEW', transform: function(match){ return parseInt( match[2] ) / Math.pow( 10,parseInt( match[1] ) )}},
        { ai: '330', regex: /^330(\d)(\d{6})/},
        { ai: '37', regex: /^37(\d{1,8})/,        parameter: 'CANT'}
      ];
    
     //walks through the code, removing recognized fields
     function parseAiByAi(code, mercancia, onError ){
        var match;
    
        if(!code)
          return;
    
        SapApplicationIdentifiers.forEach(function(AI){
          if(code.indexOf(AI.ai)==0 && AI.regex.test(code)){
            match= AI.regex.exec( code );
            if(AI.parameter){
              if(angular.isFunction(AI.transform)){
                mercancia[AI.parameter] = AI.transform(match);
              }else
                mercancia[AI.parameter]= match[1];
              if(AI.parameter=="NTGEW"){
                mercancia.NTGEW_IA= AI.ai;
              }
            }
    
            code= code.replace(match[0],'').replace(/^[\0\u001D]/,'');
            parseAiByAi(code, mercancia, onError);
          }
    
        });
      }
    
      parseAiByAi(code, mercancia, onError);
    

提交回复
热议问题