Reputation: 47
I'm trying to build a barcode scanner in android studio, using ML Kit. I have a code that works fine, it is only detecting QR codes. I wanted to make the barcode scanner to read all types of barcodes, mainly those for food products. I'm trying to get the raw values of the barcode for searching purposes in the database and displaying the concerned information to the screen.
Below are the codes that I've used to detect the QR code.
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);
}
}
Upvotes: 2
Views: 2031
Reputation: 4127
You have also a hardware problem.
The cameras of an android device usually only read the barcodes if they are focused correctly and that only happens with the rear camera since the front camera does not usually have autofocus.
Instead, QR codes are read even if they are not focused well.
In addition a normal camera is slow reading barcodes, if you want a device that reads all the codes quickly look for one with a built-in barcode reader, they are known as PDA, although they are much more expensive than a smartphone.
Upvotes: 2