In my Android App by clicking a button i'm opening a custom AlertDialog in which the user should be able to scan a barcode, the AlertDialog has in it a SurfaceView in which i start CameraSource with BarcodeDetector.
The issue is that it's not really clear to the user what he should do on that window with opened camera so i was wondering if it's possible to add like a tracker which draw a box around the barcode the user is trying to scan or something like this.
Here is my method which i call when building my AlertDialog:
private void initialiseDetectorsAndSources(SurfaceView surfaceView) {
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.ALL_FORMATS)
.build();
CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
.setRequestedPreviewSize(1600, 1024)
.setAutoFocusEnabled(true) //you should add this feature
.build();
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(pterm.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
cameraSource.start(surfaceView.getHolder());
} else {
ActivityCompat.requestPermissions(pterm.this, new
String[]{Manifest.permission.CAMERA}, 201);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
@Override
public void release() {
// Toast.makeText(getApplicationContext(), "To prevent memory leaks barcode scanner has been stopped", Toast.LENGTH_SHORT).show();
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0) {
alertFidelityTxt.post(new Runnable() {
@Override
public void run() {
if (barcodes.valueAt(0).email != null) {
alertFidelityTxt.removeCallbacks(null);
codFidelity = barcodes.valueAt(0).email.address;
alertFidelityTxt.setText(codFidelity);
toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, 150);
fidelity.setText(codFidelity);
linearFidelity.setBackgroundColor(Color.parseColor("#00796B"));
alertFidelity.dismiss();
} else {
codFidelity = barcodes.valueAt(0).displayValue;
alertFidelityTxt.setText(codFidelity);
toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, 150);
fidelity.setText(codFidelity);
linearFidelity.setBackgroundColor(Color.parseColor("#00796B"));
alertFidelity.dismiss();
}
}
});
}
}
});
}