gpt4 book ai didi

java - 我想在同一个 Activity 中停止后重新启动相机源

转载 作者:搜寻专家 更新时间:2023-11-01 08:26:47 24 4
gpt4 key购买 nike

我做了一个条形码检测器的应用程序,所以我使用 SurfaceView 来处理 camerasource。相机检测到条形码并返回其值后,我让它停止。没有任何功能可以在任何事件中重新启动相机源和表面 View ,只有一个功能是 camersource.start();

它启动了,但它在后台出现问题,我什么都看不到如何解决这个问题?并再次显示表面 View 和相机源。

这是 Activity onCreate 方法的全部代码

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

surfaceView = (SurfaceView) findViewById(R.id.cameraPreview);
txtShow = (TextView) findViewById(R.id.txtShow);

startBarcode();

}

public void startBarcode() {
barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.ALL_FORMATS)
.build();
cameraSource = new CameraSource.Builder(this, barcodeDetector)
.setRequestedPreviewSize(800, 600)
.setAutoFocusEnabled(true)
.build();

//Events

surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//Make Request Runtime Permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA}, RequestCameraPermissionId);
return;
}
try {
cameraSource.start(surfaceView.getHolder());

} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

//Make Request Runtime Permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA}, RequestCameraPermissionId);
return;
}
try {
cameraSource.start();
} catch (IOException e) {
e.printStackTrace();
}
}

@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> qrCodes = detections.getDetectedItems();
if (qrCodes.size() != 0) {
txtShow.post(new Runnable() {
@Override
public void run() {
//create vibrate
Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);


//set result for Text View
txtShow.setText(qrCodes.valueAt(0).displayValue);

// surfaceView.setTop(200);
cameraSource.stop();


final MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.barcode);
mp.start();

最佳答案

要在 cameraSource.stop() 之后重启,您需要:

cameraSource.start(surfaceView.getHolder());

关于java - 我想在同一个 Activity 中停止后重新启动相机源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44215305/

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