我试图在我的应用程序中创建条形码扫描仪。但是它没有显示相机,它在上周显示了黑屏,但仍然可以使用。我该如何解决。我的代码在这里
public class ScanBarcodeCartActivity extends AppCompatActivity {
private SurfaceView surfaceView;
private CameraSource cameraSource;
private TextView text_code,re_scan;
private Button btn_confirm_cart;
private BarcodeDetector barcodeDetector;
private String id_order;
private String phone;
private String cart,dateid,date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_barcode_cart);
surfaceView = findViewById(R.id.camera_preview);
text_code = findViewById(R.id.text_code);
btn_confirm_cart = findViewById(R.id.confirm_cart);
re_scan = findViewById(R.id.re_scan);
barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(
Barcode.QR_CODE
).build();
cameraSource = new CameraSource.Builder(this,barcodeDetector)
.setRequestedPreviewSize(640,480).setAutoFocusEnabled(true).build();
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
}
try {
cameraSource.start(holder);
}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> qrcode = detections.getDetectedItems();
if (qrcode.size() != 0) {
text_code.post(new Runnable() {
@Override
public void run() {
text_code.setText(qrcode.valueAt(0).displayValue);
}
});
}
}
});
} }
我尝试了许多方法,但仍然不能。 我不知道是因为相机还是表面视图。 并感谢您的建议。