Android cameraSource.stop() causing app to freeze(Android cameraSource.top()导致应用程序冻结)
问题描述
我正在构建一个应用程序,有一个使用谷歌视觉API的二维码扫描仪。在读取二维码后,我无法停止摄像头。流程为MainActivity -> QrActivity
收到二维码检测后,应用程序应返回主活动。
如果我不调用cameraSource.release()
,它工作得很好,但设备会发热很多,并且对电池耗尽有很大影响。然而,如果我释放摄像头信号源,mainActivity将变得没有响应,应用程序将崩溃。
为什么它变得无响应?释放摄像信号源的正确位置在哪里?
季度活动
@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个月了,但我只是偶然发现了同样的问题并解决了它。ReceiveDettions方法中的代码在不同的线程上运行,因此,如果您想要执行需要UI线程的操作,则需要从处理程序发送它:
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
cameraSource.release();
}
});
这篇关于Android cameraSource.top()导致应用程序冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android cameraSource.top()导致应用程序冻结
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01