I am building an app that has a qr scanner using the google vision api. I am having trouble stopping the camera after the qr code is read. the flow is MainActivity -> QrActivity
once the qr-code received a detection the app should return to the main activity.
If i do not call cameraSource.release()
it works fine but the device heats up a lot and has a significant impact on battery drain. however if i release the camera source the mainActivity becomes un-responsive and the app will crash.
Why is it becoming unresponsive? and where is the correct place to release the camera source?
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 Class
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
}
}
it's been 3 months but I just stumbled on the same problem and figure it out. The code inside the receiveDetections method is running on a different thread so if you want to do something that needs the ui thread you need to post it from a handler:
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
cameraSource.release();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With