Buon sera a tutti st tentando di leggere i barcode con la mia app questa è la classe:
codice:
ublic class ScanBarcodeActivity extends Activity { SurfaceView cameraPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_barcode);
cameraPreview = (SurfaceView) findViewById(R.id.camera_preview);
createCameraSource();
}
private void createCameraSource() {
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).build();
final CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
.setFacing(Camera.CameraInfo.CAMERA_FACING_BACK)
.setAutoFocusEnabled(true)
.setRequestedPreviewSize(1600, 1024)
.build();
cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (ActivityCompat.checkSelfPermission(ScanBarcodeActivity.this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
try {
cameraSource.start(cameraPreview.getHolder());
} 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() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size()>0)
{
Intent intent = new Intent();
intent.putExtra("barcode",barcodes.valueAt(0)); //Legge il barcode dell'array
setResult(CommonStatusCodes.SUCCESS,intent);
finish();
}
}
});
}
}
Sto facendo il debug ma alla linea 46 mi dice che "No such instance field: 'cameraPreview'" e la camera non parte, dove sbaglio?
Vedi allegato immagine