来自 googles mlkit 的 Barcode Scanner 无法识别任何条形码
the Barcode Scanner from googles mlkit doesn't recognize any barcodes
所以我按照 google 的条码扫描器教程 (this one) 进行了操作,二维码扫描非常有效。唯一的问题是我不需要二维码,而是条形码。但它们不起作用。它没有检测到任何东西。我尝试了多个在线条码和家里的条码,但 none 被识别为条码。
这是我的 activity 中处理图像和扫描仪的代码:
public void btnClick(View v) {
Intent imageTakeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(imageTakeIntent.resolveActivity(getPackageManager())!=null){
startActivityForResult(imageTakeIntent, 101);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==101 && resultCode==RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
image = InputImage.fromBitmap(imageBitmap, 0);
barCodeScanning();
}
}
public void barCodeScanning(){
BarcodeScannerOptions options =
new BarcodeScannerOptions.Builder()
.setBarcodeFormats(
Barcode.FORMAT_CODE_128,
Barcode.FORMAT_CODE_93,
Barcode.FORMAT_CODE_93,
Barcode.FORMAT_CODABAR,
Barcode.FORMAT_EAN_13,
Barcode.FORMAT_EAN_8,
Barcode.FORMAT_ITF,
Barcode.FORMAT_UPC_A,
Barcode.FORMAT_UPC_E,
Barcode.FORMAT_PDF417,
Barcode.FORMAT_DATA_MATRIX,
Barcode.FORMAT_QR_CODE,
Barcode.FORMAT_AZTEC)
.build();
BarcodeScanner scanner = BarcodeScanning.getClient();
Task<List<Barcode>> result = scanner.process(image)
.addOnSuccessListener(new OnSuccessListener<List<Barcode>>() {
@Override
public void onSuccess(List<Barcode> barcodes) {
System.out.println(barcodes.size());
for (Barcode barcode: barcodes) {
Rect bounds = barcode.getBoundingBox();
Point[] corners = barcode.getCornerPoints();
String rawValue = barcode.getRawValue();
System.out.println(rawValue);
int valueType = barcode.getValueType();
System.out.println(valueType);
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
}
控制台对我来说看起来不错,除了标记为红色的行 E/libc: Access denied finding property "ro.hardware.chipname"
但在 google 上没有显示任何有用的结果。
这是完整的控制台输出:
02/26 18:29:10: Launching 'app' on HMD Global Nokia 5.3.
Install successfully finished in 2 s 230 ms.
$ adb shell am start -n "com.project.kuecheninventar/com.project.kuecheninventar.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 28314 on device 'hmd_global-nokia_5_3-N0AA003687KA2700363'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Perf: Connecting to perf service.
W/kuecheninventa: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
I/AdrenoGLES: QUALCOMM build : cfd6c90, Id60e6598a1
Build Date : 01/27/20
OpenGL ES Shader Compiler Version: EV031.27.05.03
Local Branch : mybranchea40f85b-21cb-f1fb-b0c0-5cb90179c3e0
Remote Branch : quic/gfx-adreno.lnx.1.0.r91-rel
Remote Branch : NONE
Reconstruct Branch : NOTHING
Build Config : S P 8.0.12 AArch64
I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
W/Gralloc3: mapper 3.x is not supported
D/TransportRuntime.JobInfoScheduler: Scheduling upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) with jobId=-1883559869 in 86400000ms(Backend next call timestamp 0). Attempt 1
I/DynamiteModule: Considering local module com.google.mlkit.dynamite.barcode:10000 and remote module com.google.mlkit.dynamite.barcode:0
Selected local version of com.google.mlkit.dynamite.barcode
I/tflite: Initialized TensorFlow Lite runtime.
I/native: barcode_detector_client.cc:239 Not using NNAPI
E/libc: Access denied finding property "ro.hardware.chipname"
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
I/System.out: 0
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
行System.out.println(barcodes.size());
只打印0,这意味着条码列表是空的。我是不是做错了什么或者为什么二维码可以识别但是没有条形码?
我忘记了这个问题,因为我通过其他方式解决了。所以这是我的 zxing 解决方案:
在应用程序中 built.gradle 使用 implementation 'com.journeyapps:zxing-android-embedded:4.1.0'
public class BarCodeScanner extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scan();
}
private void scan() {
IntentIntegrator i = new IntentIntegrator(this);
i.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
i.setCaptureActivity(CaptureActivitaet.class);
i.setOrientationLocked(false);
i.setBeepEnabled(false);
i.setPrompt("Scanning");
i.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
IntentResult res = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (res != null) {
if (res.getContents() != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(res.getContents());
builder.setTitle(getString(R.string.scanComplete));
builder.setPositiveButton(R.string.ok, (dialogInterface, i) -> {
setResult(RESULT_OK, new Intent().putExtra("code", res.getContents()));
finish();
}).setNegativeButton(R.string.scanAgain, (dialogInterface, i) -> scan());
AlertDialog dialog = builder.create();
dialog.show();
} else {
Toast.makeText(this, "No Results", Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
捕获 activity 只是一个空的 activity public class CaptureActivitaet extends CaptureActivity {}
尽管它必须像这样在清单文件中配置:
<activity
android:name="com.project.Grocerly.UI.CaptureActivitaet"
android:screenOrientation="fullSensor"
android:stateNotNeeded="true"
android:windowSoftInputMode="stateAlwaysHidden" />
这会打开一个全屏相机 activity,它会根据 scan()
方法中 i.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
设置的类型扫描代码。如果您使用 setCaptureActivity(this.class)
并在清单文件中正确配置它,则可能会跳过额外的捕获 activity 但我不确定。
因为这是一个大学项目,所以这段代码不是很漂亮,但它可以工作,这是我当时主要关心的问题。
所以我按照 google 的条码扫描器教程 (this one) 进行了操作,二维码扫描非常有效。唯一的问题是我不需要二维码,而是条形码。但它们不起作用。它没有检测到任何东西。我尝试了多个在线条码和家里的条码,但 none 被识别为条码。
这是我的 activity 中处理图像和扫描仪的代码:
public void btnClick(View v) {
Intent imageTakeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(imageTakeIntent.resolveActivity(getPackageManager())!=null){
startActivityForResult(imageTakeIntent, 101);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==101 && resultCode==RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
image = InputImage.fromBitmap(imageBitmap, 0);
barCodeScanning();
}
}
public void barCodeScanning(){
BarcodeScannerOptions options =
new BarcodeScannerOptions.Builder()
.setBarcodeFormats(
Barcode.FORMAT_CODE_128,
Barcode.FORMAT_CODE_93,
Barcode.FORMAT_CODE_93,
Barcode.FORMAT_CODABAR,
Barcode.FORMAT_EAN_13,
Barcode.FORMAT_EAN_8,
Barcode.FORMAT_ITF,
Barcode.FORMAT_UPC_A,
Barcode.FORMAT_UPC_E,
Barcode.FORMAT_PDF417,
Barcode.FORMAT_DATA_MATRIX,
Barcode.FORMAT_QR_CODE,
Barcode.FORMAT_AZTEC)
.build();
BarcodeScanner scanner = BarcodeScanning.getClient();
Task<List<Barcode>> result = scanner.process(image)
.addOnSuccessListener(new OnSuccessListener<List<Barcode>>() {
@Override
public void onSuccess(List<Barcode> barcodes) {
System.out.println(barcodes.size());
for (Barcode barcode: barcodes) {
Rect bounds = barcode.getBoundingBox();
Point[] corners = barcode.getCornerPoints();
String rawValue = barcode.getRawValue();
System.out.println(rawValue);
int valueType = barcode.getValueType();
System.out.println(valueType);
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
}
控制台对我来说看起来不错,除了标记为红色的行 E/libc: Access denied finding property "ro.hardware.chipname"
但在 google 上没有显示任何有用的结果。
这是完整的控制台输出:
02/26 18:29:10: Launching 'app' on HMD Global Nokia 5.3.
Install successfully finished in 2 s 230 ms.
$ adb shell am start -n "com.project.kuecheninventar/com.project.kuecheninventar.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 28314 on device 'hmd_global-nokia_5_3-N0AA003687KA2700363'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Perf: Connecting to perf service.
W/kuecheninventa: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
I/AdrenoGLES: QUALCOMM build : cfd6c90, Id60e6598a1
Build Date : 01/27/20
OpenGL ES Shader Compiler Version: EV031.27.05.03
Local Branch : mybranchea40f85b-21cb-f1fb-b0c0-5cb90179c3e0
Remote Branch : quic/gfx-adreno.lnx.1.0.r91-rel
Remote Branch : NONE
Reconstruct Branch : NOTHING
Build Config : S P 8.0.12 AArch64
I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
W/Gralloc3: mapper 3.x is not supported
D/TransportRuntime.JobInfoScheduler: Scheduling upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) with jobId=-1883559869 in 86400000ms(Backend next call timestamp 0). Attempt 1
I/DynamiteModule: Considering local module com.google.mlkit.dynamite.barcode:10000 and remote module com.google.mlkit.dynamite.barcode:0
Selected local version of com.google.mlkit.dynamite.barcode
I/tflite: Initialized TensorFlow Lite runtime.
I/native: barcode_detector_client.cc:239 Not using NNAPI
E/libc: Access denied finding property "ro.hardware.chipname"
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
I/System.out: 0
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
行System.out.println(barcodes.size());
只打印0,这意味着条码列表是空的。我是不是做错了什么或者为什么二维码可以识别但是没有条形码?
我忘记了这个问题,因为我通过其他方式解决了。所以这是我的 zxing 解决方案:
在应用程序中 built.gradle 使用 implementation 'com.journeyapps:zxing-android-embedded:4.1.0'
public class BarCodeScanner extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scan();
}
private void scan() {
IntentIntegrator i = new IntentIntegrator(this);
i.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
i.setCaptureActivity(CaptureActivitaet.class);
i.setOrientationLocked(false);
i.setBeepEnabled(false);
i.setPrompt("Scanning");
i.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
IntentResult res = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (res != null) {
if (res.getContents() != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(res.getContents());
builder.setTitle(getString(R.string.scanComplete));
builder.setPositiveButton(R.string.ok, (dialogInterface, i) -> {
setResult(RESULT_OK, new Intent().putExtra("code", res.getContents()));
finish();
}).setNegativeButton(R.string.scanAgain, (dialogInterface, i) -> scan());
AlertDialog dialog = builder.create();
dialog.show();
} else {
Toast.makeText(this, "No Results", Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
捕获 activity 只是一个空的 activity public class CaptureActivitaet extends CaptureActivity {}
尽管它必须像这样在清单文件中配置:
<activity
android:name="com.project.Grocerly.UI.CaptureActivitaet"
android:screenOrientation="fullSensor"
android:stateNotNeeded="true"
android:windowSoftInputMode="stateAlwaysHidden" />
这会打开一个全屏相机 activity,它会根据 scan()
方法中 i.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
设置的类型扫描代码。如果您使用 setCaptureActivity(this.class)
并在清单文件中正确配置它,则可能会跳过额外的捕获 activity 但我不确定。
因为这是一个大学项目,所以这段代码不是很漂亮,但它可以工作,这是我当时主要关心的问题。