gpt4 book ai didi

java - Android cameraSource.stop() 导致应用程序卡住

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:43 27 4
gpt4 key购买 nike

我正在使用 google vision api 构建一个具有二维码扫描仪的应用程序。读取二维码后我无法停止相机。流程是 MainActivity -> QrActivity一旦二维码收到检测,应用程序应返回主 Activity 。

如果我不调用 cameraSource.release() 它工作正常,但设备会发热很多并且对电池消耗有很大影响。但是,如果我释放相机源,mainActivity 将变得无响应,应用程序将崩溃。

为什么它变得无响应?释放相机源的正确位置在哪里?

QrActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
cancelBtn = (Button) findViewById(R.id.cancel_button);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});

new QrReader(this);
}

QrReader 类

public class QrReader {

private static final String TAG = "QrReader";

private SurfaceView cameraView;
private TextView barcodeInfo;
private BarcodeDetector barcodeDetector;
private CameraSource cameraSource;
private Activity mActivity;
private AccessPointCredentials barCodeData;

public QrReader(Activity activity) {
this.mActivity = activity;

cameraView = (SurfaceView) mActivity.findViewById(R.id.camera_view);
barcodeInfo = (TextView) mActivity.findViewById(R.id.code_info);

barcodeDetector =
new BarcodeDetector.Builder(mActivity)
.setBarcodeFormats(Barcode.QR_CODE)
.build();

cameraSource = new CameraSource
.Builder(mActivity, barcodeDetector)
.setAutoFocusEnabled(true)
.build();

cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {

@Override
public void surfaceCreated(SurfaceHolder holder) {

cameraSource = new CameraSource
.Builder(mActivity, barcodeDetector)
.setAutoFocusEnabled(true)
.setFacing(0)
.build();
try {
cameraSource.start(cameraView.getHolder());

} catch (Exception ioe) {
ioe.printStackTrace();
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// Log.i(TAG, "surfaceDestroyed: stopping camera Source");

// cameraSource.release();
}
});

barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
@Override
public void release() {
Log.i(TAG, "release: ");
}

@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barCodes = detections.getDetectedItems();

if (barCodes.size() != 0) {

Log.i(TAG, "received a Barcode");
barcodeInfo.post(new Runnable() { // Use the post method of the TextView
public void run() {
barcodeInfo.setText(barCodes.valueAt(0).displayValue);

}
});
Gson g = new Gson();
try {
barCodeData = g.fromJson(barCodes.valueAt(0).rawValue, AccessPointCredentials.class);
} catch (Exception e) {
barCodeData = new AccessPointCredentials();
barCodeData.setSsid(barCodes.valueAt(0).rawValue);
barCodeData.setPass(null);
e.printStackTrace();
}

connectToWifi(barCodeData);

// CameraSource.release causes app to freeze

// cameraSource.release();
}
}
});
}

private void connectToWifi(final AccessPointCredentials credentials) {

//wificonnect code

}

}

最佳答案

已经 3 个月了,但我偶然发现了同样的问题并解决了这个问题。 receiveDetections 方法中的代码在不同的线程上运行,所以如果你想做一些需要 ui 线程的事情,你需要从处理程序发布它:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
cameraSource.release();
}
});

关于java - Android cameraSource.stop() 导致应用程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39641111/

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