本文介紹了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()導致應用程序凍結的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,