English 中文(简体)
摘录GS1公司
原标题:Extracting the GS1 Company Prefix from a GTIN

我试图让公司从使用dart的条码中预先确定:

String extractCompanyPrefix(String gtin) {
  // Ensure the GTIN is a string to handle leading zeros and perform string operations
  String gtinStr = gtin.padLeft(14,  0 );

  String prefix = gtinStr.substring(0, 8);
  // remove leading zeros
  prefix = prefix.replaceFirst(RegExp(r ^0+ ),   );
  return prefix;
}

void main(List<String> arguments) {
  if (arguments.isEmpty) {
    print( Please provide a barcode. );
    return;
  }

  String barcode = arguments.first;
  String companyPrefix = extractCompanyPrefix(barcode);
  print( Company prefix: $companyPrefix );
}

❯ dart run dart/company_prefix_checker.dart 8710398527905
Company prefix: 8710398
❯ dart run dart/company_prefix_checker.dart 40111445
Company prefix: 40

这样做是否正确,会形成一个准确的GS1公司预设点?


Updated code to add @Terry Burton recommendations:

import  dart:convert ;
import  dart:io ;
import  package:path/path.dart  as path;
import  lib/application_identifiers.dart ;
import  lib/gcp_length.dart ;

Future<String> extractCompanyPrefix(
    String barcode, Map<String, int> gcpLengths) async {
  barcode = barcode.padLeft(12,  0 );
  for (var prefix in gcpLengths.keys) {
    if (barcode.startsWith(prefix)) {
      print(
           Found GCP prefix format: $prefix with length: ${gcpLengths[prefix]} );
      return barcode.substring(0, gcpLengths[prefix]!);
    }
  }

  var gai = await GS1ApplicationIdentifier.extract(barcode);


  var result = getGCPLength(gai.ai, barcode.substring(gai.ai.length));

  if (result !=  There is no matching value. Try GEPIR (https://gepir.gs1.org/) or contact local GS1 MO. ) {
    return result;
  }

  return "Invalid Barcode or GCP Not Found";
}

Future<Map<String, int>> loadGcpLengths(String filePath) async {
  try {
    var file = File(filePath);
    var contents = await file.readAsString();
    var jsonData = json.decode(contents);

    Map<String, int> gcpLengths = {};
    for (var entry in jsonData[ GCPPrefixFormatList ][ entry ]) {
      gcpLengths[entry[ prefix ]] = entry[ gcpLength ];
    }

    return gcpLengths;
  } catch (e) {
    throw Exception( Failed to load GCP length data from file: $e );
  }
}

void main(List<String> arguments) async {
  if (arguments.isEmpty) {
    print( Please provide a barcode. );
    return;
  }

  // Specify the path to your local  gcpprefixformatlist.json  file
  var jsonFilePath =
      path.join(Directory.current.path,  lib/gcpprefixformatlist.json );

  String barcode = arguments.first;
  try {
    Map<String, int> gcpLengths = await loadGcpLengths(jsonFilePath);
    String companyPrefix = await extractCompanyPrefix(barcode, gcpLengths);
    print( Company prefix: $companyPrefix );
  } catch (e) {
    print( Error: $e );
  }
}

lib/application_identifiers.dart:


import  dart:convert ;
import  dart:io ;

class GS1ApplicationIdentifier {
  final String ai;
  final String description;
  final String dataTitle;
  final bool fnc1Required;
  final String format;
  final String pattern;

  const GS1ApplicationIdentifier({
    required this.ai,
    required this.description,
    required this.dataTitle,
    required this.fnc1Required,
    required this.format,
    required this.pattern,
  });

  static Future<GS1ApplicationIdentifier> extract(String value) async {
    final applicationIdentifiers = await _loadApplicationIdentifiers();
    for (var ai in applicationIdentifiers) {
      if (value.startsWith(ai.ai)) {
        return ai;
      }
    }
    throw FormatException( Failed to get GS1 Application Identifier from $value. );
  }

  @override
  String toString() {
    return  ($ai) ;
  }

  static Future<List<GS1ApplicationIdentifier>> _loadApplicationIdentifiers() async {
    // final jsonString = await rootBundle.loadString( lib/application_identifiers.json );
    final jsonString = await File( lib/_application_identifiers.json ).readAsString();
    final List<dynamic> jsonList = json.decode(jsonString);
    return jsonList.map((json) => GS1ApplicationIdentifier.fromJson(json)).toList();
  }

  factory GS1ApplicationIdentifier.fromJson(Map<String, dynamic> json) {
    return GS1ApplicationIdentifier(
      ai: json[ ai ] as String,
      description: json[ description ] as String,
      dataTitle: json[ data_title ] as String,
      fnc1Required: json[ fnc1_required ] as bool,
      format: json[ format ] as String,
      pattern: json[ pattern ] as String,
    );
  }
}

lib/gcp_length.dart:

import  dart:convert ;
import  package:http/http.dart  as http;

final Map<String, String> gs1KeyRegEx = {
   00 : r ^(d{18})$ ,
   01 : r ^(d{14})$ ,
   253 : r ^(d{13})([x21-x22x25-x2Fx30-x39x3A-x3Fx41-x5Ax5Fx61-x7A]{0,17})$ ,
   255 : r ^(d{13})(d{0,12})$ ,
   401 : r ^([x21-x22x25-x2Fx30-x39x3A-x3Fx41-x5Ax5Fx61-x7A]{0,30})$ ,
   402 : r ^(d{17})$ ,
   414 : r ^(d{13})$ ,
   417 : r ^(d{13})$ ,
   8003 : r ^(d{14})([x21-x22x25-x2Fx30-x39x3A-x3Fx41-x5Ax5Fx61-x7A]{0,16})$ ,
   8004 : r ^([x21-x22x25-x2Fx30-x39x3A-x3Fx41-x5Ax5Fx61-x7A]{0,30})$ ,
   8006 : r ^(d{14})(d{2})(d{2})$ ,
   8010 : r ^([x23x2Dx2Fx30-x39x41-x5A]{0,30})$ ,
   8017 : r ^(d{18})$ ,
   8018 : r ^(d{18})$ ,
};

final Map<String, bool> keyStartsWithGCP = {
   00 : false,
   01 : false,
   253 : true,
   255 : true,
   401 : true,
   402 : true,
   414 : true,
   417 : true,
   8003 : false,
   8004 : true,
   8006 : false,
   8010 : true,
   8017 : true,
   8018 : true,
};


List<Map<String, dynamic>> gcpDict = [];

Future<void> fetchGcpData() async {
  final response = await http.get(Uri.parse( https://www.gs1.org/sites/default/files/docs/gcp_length/gcpprefixformatlist.json ));
  if (response.statusCode == 200) {
    final allGCPs = jsonDecode(response.body) as Map<String, dynamic>;
    gcpDict = List<Map<String, dynamic>>.from(allGCPs["GCPPrefixFormatList"]["entry"]);
  } else {
    throw Exception( Failed to fetch GCP Length Table data. );
  }
}

String getGCPLength(String aI, String gs1Key) {
  // Check if GS1 Key complies with its corresponding RegEx
  final regex = RegExp(gs1KeyRegEx[aI] ??   );
  if (!regex.hasMatch(gs1Key)) {
    return  The GS1 Key has an incorrect length or impermissible characters. ;
  }

  String gcpLength =   ;
  int j = 12;

  // Normalize input string
  if (keyStartsWithGCP[aI] ?? false) {
    gs1Key =  0  + gs1Key;
  }
  // Determine GCP length
  while (j > 2 && gcpLength.isEmpty) {
    for (var entry in gcpDict) {
      if (entry["prefix"].length == j && gs1Key.startsWith(entry["prefix"])) {
        gcpLength = entry["gcpLength"].toString();
        return gcpLength;
      }
    }
    j -= 1;
  }

  return  There is no matching value. Try GEPIR (https://gepir.gs1.org/) or contact local GS1 MO. ;
}

Future<void> main() async {
  try {
    await fetchGcpData();
    // Example usage
    String gcpLength = getGCPLength( 01 ,  01234567891234 );
    print(gcpLength);
  } catch (e) {
    print( Error: $e );
  }
}


❯ dart run company_prefix_checker.dart 8710398527905
Found GCP prefix format: 8710 with length: 7
Company prefix: 8710398
❯ dart run company_prefix_checker.dart 40111445
Found GCP prefix format: 00004 with length: 7
Company prefix: 0000401

该守则将检索。 https://www.gs1.org/docs/gcp_length/gcpprefixatlist.json 与其他智慧相匹配,将失败于reg和大赦国际。

这是正确的结果吗?

最佳回答

这种正确方式和结果吗?

页: 1

GS1公司(GCP)的固定时间为6至12位数。

数据本身没有具体规定全球氯乙烯的长度。 全球气候方案的一个实例的长短完全由全球气候方案区域发行人颁发许可证的GS1组织“MO”决定。 几乎所有的卫生部都向全球全球一级汇报了各自的分配办法,全球一级全球一级全球一级有时公布全球化学品统一分类标签的总体情况。

You cannot separate out the GCP from the remaining data without reference to the prefix length tables that are provided in XML and JSON format, here: https://www.gs1.org/standards/bc-epc-interop

这些数据并不及时,而且质量的准确性也不尽相同,这在很大程度上取决于签发全球化学品统一分类标签的GS1成员组织的勤奋。

问题回答

暂无回答




相关问题
USB barcode reader in Qt4

I was wondering how could I integrate a USB barcode scanner into a Qt application, as well as a barcode printer. I looked for tutorials but didn t find anything interesting out there. Any ideas? ...

Best Bar Code Scanner for both Web Form/ Win Form input

I have an application that prints eTickets. Originally I used the cool 2-D bar code because it allowed me to use the GUID as the code. However scanners that can scan 2-D bar codes are way too ...

How to display barcode in iPhone

I need to show barcode string(example :1001847983) into barcode image in A UIView. Does the iPhone SDK supports barcode fonts? I don t need reading the barcode, I just want to show the string in ...

热门标签