Scanning GS1-128 barcode with expo camera barcode scnner

Expo Details:

  1. EXPO Version: 46.0.0
  2. Expo Camera Version: 12.3.0
  3. Expo Barcode Scanner: 11.4.0
  4. Platforms(Android/iOS/web/all): Android

Hi, we are using expo camera scanner for scanning barcodes in our react native mobile app. Our user is facing an issue while scanning barcode of type GS1-128 in android (working fine in case of iOS). It is appending ]C1 at start of scanned data. Is there any way to tackle barcodes of type GS1-128 or maybe a way due to which i could find out that the scanned barcode type is GS1-128 so that i could remove ]C1 from start

Hi @tabish1122

When I ran into this problem I worked around it like this:

      let gs1;
      if (data.charAt(0) == "\u001d") {
        gs1 = bark(data.substring(1));
      } else {
        gs1 = bark(data);
      }

EDIT: Actually, I think the above might have been for a similar, but different issue.

If I look here:

it says that ]C1 is the Symbol ID for GS1-128 barcodes:

If I use bark-js to interpret the barcode from the above URL I get this:

{
  "symbology": "GS1-128",
  "elements": [
    {
      "ai": "01",
      "title": "GTIN",
      "value": "09501101530003",
      "raw": "09501101530003"
    },
    {
      "ai": "17",
      "title": "USE BY OR EXPIRY",
      "value": "2014-07-04",
      "raw": "140704"
    },
    {
      "ai": "10",
      "title": "BATCH/LOT",
      "value": "AB-123",
      "raw": "AB-123"
    }
  ],
  "originalBarcode": "]C101095011015300031714070410AB-123"
}

Hi, @wodin Thanks for you reply

Expo camera returns string so we cannot compare data.charAt(0) == “\u001d”
i tried comparing data.charAt(0) == “\u001d” and it returns false because expo camera does not provides raw data, it returns string

I know that it is GS1-128 barcode but there is no way to identify it with expo camera that it is of type GS1-128. I cannot directly remove ]C1 from start of scanned data because maybe user intentionally generated barcode that appends ]C1 at start

data is a string. But I was not thinking clearly enough when I sent my last message.

What are you using to parse the GS1 data? I was using bark-js.

If you parse the string with bark-js then it just treats the ]C1 at the start as an indicator that this is a GS1-128 barcode rather than e.g. a datamatrix.

For example you can test this with NodeJS, but it works the same in React Native:

/tmp$ mkdir gs1-test
/tmp$ cd gs1-test
/tmp/gs1-test$ yarn init
yarn init v1.22.19
question name (gs1-test): 
question version (1.0.0): 
question description: 
question entry point (index.js): 
question repository url: 
question author: 
question license (MIT): 
question private: 
success Saved package.json
Done in 3.28s.
/tmp/gs1-test$ yarn add bark-js
yarn add v1.22.19
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ bark-js@1.2.1
info All dependencies
└─ bark-js@1.2.1
Done in 1.60s.
/tmp/gs1-test$ cat >index.js <<EOF
const bark = require('bark-js');
const data = ']C101095011015300031714070410AB-123';
const gs1Data = bark(data);
console.log(JSON.stringify(gs1Data, null, 2));
EOF
/tmp/gs1-test$ node index.js 
{
  "symbology": "GS1-128",
  "elements": [
    {
      "ai": "01",
      "title": "GTIN",
      "value": "09501101530003",
      "raw": "09501101530003"
    },
    {
      "ai": "17",
      "title": "USE BY OR EXPIRY",
      "value": "2014-07-04",
      "raw": "140704"
    },
    {
      "ai": "10",
      "title": "BATCH/LOT",
      "value": "AB-123",
      "raw": "AB-123"
    }
  ],
  "originalBarcode": "]C101095011015300031714070410AB-123"
}

If you remove the ]C1 from the start of the string, it gives you basically the same output, except that the symbology is "unknown".

@wodin I completely understand your point, but main issue is that our client can generate barcode and intentionally can append ]C1 at start, in that case ]C1 should not be removed, that is why we need a type with barcode that this barcode is of type GS1-128, but in expo camera type of scanned barcode is shown as Code-128
Here is an example:

User generated barcode: ]C1123, type: Any type other than GS1-128:
Barcode result should be ]C1123 because user added it intentionally

User generated barcode: 123, type: GS1-128, scanned data: ]C1123:
Barcode result should be 123, ]C1 should be removed becasue it is of type GS1-128

bark-js will check the string and if it finds ]C1 at start it will say that it is of type GS1-128 even if it is of another type and user appended it intentionally

In that case this is not something that Expo can help you with.

Expo is only returning what the barcode represents. Expo is not adding the ]C1. It is actually part of the barcode data.

If you find a generic Code 128 barcode generator and generate a barcode for:
12231209
and scan it, you will get the string: "12231209". If you parse that with bark-js, you will get:

{
  "symbology": "unknown",
  "elements": [
    {
      "ai": "12",
      "title": "DUE DATE",
      "value": "2023-12-09",
      "raw": "231209"
    }
  ],
  "originalBarcode": "12231209"
}

But if you generate a barcode for ]C112231209, and scan it, you will get the string: "]C112231209". If you parse that with bark-js, you will get:

{
  "symbology": "GS1-128",
  "elements": [
    {
      "ai": "12",
      "title": "DUE DATE",
      "value": "2023-12-09",
      "raw": "231209"
    }
  ],
  "originalBarcode": "]C112231209"
}

If your users can send any data in a barcode, then how will you know if it’s a GS1 barcode or not? If you know it’s a GS1 barcode, you can parse it with bark-js. If it starts with ]C1, then that is a valid GS1-128 barcode that specifies the symbology. If it does not start with ]C1, it is still valid, but it does not specify the symbology.

If you do not know whether the user intended to scan a GS1 barcode, then you have to guess, or ask the user what type of barcode it is.

If your users sometimes scan barcodes that happen to start with ]C1 and also happen to be parseable as GS1 data, but are not actually GS1 data, then there’s no way to automatically know what it is.

So basically, it seems that the problem is that on iOS, the native barcode scanning code is removing the ]C1 at the start of the data while on Android it is not. If you know the barcode is a GS1 barcode, then this should not matter. If you don’t know what type of barcode it is, you’ll have to decide what to do in that case.

I hope that helps.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.