gpt4 book ai didi

java - 条形码扫描仪使用 ML Kit 仅读取二维码

转载 作者:行者123 更新时间:2023-11-29 18:25:17 25 4
gpt4 key购买 nike

我正在尝试使用 ML Kit 在 android studio 中构建条形码扫描器。我有一个工作正常的代码,它只检测二维码。我想让条码扫描仪能够读取所有类型的条码,主要是食品条码。我正在尝试获取条形码的原始值,以便在数据库中进行搜索并将相关信息显示到屏幕上。

下面是我用来检测二维码的代码。

public class MainActivity extends AppCompatActivity {
CameraView camera_view;
boolean isDetected = false;
private Button btn_start_again;

FirebaseVisionBarcodeDetectorOptions options;
FirebaseVisionBarcodeDetector detector;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Dexter.withActivity(this)
.withPermissions(new String [] {Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO})
.withListener(new MultiplePermissionsListener() {

@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {

setUpCamera();

}

@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {

Toast.makeText(getApplicationContext(),"You must accept the permissions",Toast.LENGTH_LONG).show();

}
}).check();

}

private void setUpCamera() {

btn_start_again =(Button)findViewById(R.id.btn_again);
btn_start_again.setEnabled(false);

btn_start_again.setOnClickListener((View v) -> isDetected = !isDetected);

camera_view = (CameraView) findViewById(R.id.cameraview);
camera_view.setLifecycleOwner(this);

camera_view.addFrameProcessor(new FrameProcessor() {
@Override
public void process(@NonNull Frame frame) {

processImage(getVisionImageFromFrame(frame));


}
});

FirebaseVisionBarcodeDetectorOptions options =
new FirebaseVisionBarcodeDetectorOptions.Builder()
.setBarcodeFormats(
FirebaseVisionBarcode.FORMAT_QR_CODE,
FirebaseVisionBarcode.FORMAT_AZTEC)
.build();
detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options);

}

private void processImage(FirebaseVisionImage image) {

if(!isDetected){

detector.detectInImage(image)
.addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
@Override
public void onSuccess(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {

processResult(firebaseVisionBarcodes);

}
})

.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {

Toast.makeText(getApplicationContext(),"e.getMessage()",Toast.LENGTH_LONG).show();

}
});


}

}

private void processResult(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {

if(firebaseVisionBarcodes.size() > 0 ){

isDetected = true;
btn_start_again.setEnabled(isDetected);

for (FirebaseVisionBarcode item:firebaseVisionBarcodes){

Rect bounds = item.getBoundingBox();
Point[] corners = item.getCornerPoints();

String rawValue = item.getRawValue();

int value_type = item.getValueType();
switch (value_type){
case FirebaseVisionBarcode.TYPE_TEXT:
{
createDialog(item.getRawValue());
}
break;
case FirebaseVisionBarcode.TYPE_URL:
{

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.getRawValue()));
startActivity(intent);

}
case FirebaseVisionBarcode.TYPE_CONTACT_INFO:
{

String info = new StringBuilder("Name:")
/*.append(item.getContactInfo().getName().getFormattedName())
.append("\n")*/
.append("Address")
.append(item.getContactInfo().getAddresses().get(0).getAddressLines())
.append("\n")
.append("Email:")
.append(item.getContactInfo().getEmails().get(0).getAddress())
.toString();
createDialog(info);

}
break;

default:
{

createDialog(rawValue);

}
break;


}

}

}

}

private void createDialog(String text) {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setMessage(text);
builder.setPositiveButton("OK", ((DialogInterface dialog, int i) -> dialog.dismiss()));

AlertDialog dialog = builder.create();
dialog.show();

}

private FirebaseVisionImage getVisionImageFromFrame(Frame frame) {

byte[] data = frame.getData();
FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder()
.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
.setHeight(frame.getSize().getHeight())
.setWidth(frame.getSize().getWidth())
.build();
//.setRotation(frame.getRotation())

return FirebaseVisionImage.fromByteArray(data,metadata);
}
}

最佳答案

您还有硬件问题。

Android 设备的摄像头通常只有在正确对焦的情况下才会读取条形码,而且只有后置摄像头才会发生这种情况,因为前置摄像头通常没有自动对焦功能。

相反,即使没有很好地聚焦,QR 码也会被读取。

此外,普通相机读取条码速度较慢,如果您想要一种能够快速读取所有代码的设备,请寻找带有内置条码阅读器的设备,它们被称为 PDA,尽管它们比智能手机贵得多.

关于java - 条形码扫描仪使用 ML Kit 仅读取二维码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59436192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com