@@ -90,19 +90,17 @@ public void onResume() {
9090 @ Override
9191 protected @ Nullable Camera onRunBackground () {
9292 try {
93- if (cameraId >= 0 ) {
94- return Camera .open (cameraId );
95- } else {
96- return null ;
97- }
93+ return Camera .open (cameraId );
9894 } catch (Exception e ) {
95+ Log .w (TAG , e );
9996 return null ;
10097 }
10198 }
10299
103100 @ Override
104101 protected void onPostMain (@ Nullable Camera camera ) {
105102 if (camera == null ) {
103+ Log .w (TAG , "tried to open camera but got null" );
106104 if (listener != null ) listener .onCameraFail ();
107105 return ;
108106 }
@@ -132,17 +130,23 @@ public void onPause() {
132130 if (!started ) return ;
133131 started = false ;
134132 Log .w (TAG , "onPause() queued" );
135- final Optional <Camera > cameraToDestroy = camera ;
136133
137134 enqueueTask (new SerialAsyncTask <Void >() {
135+ private Optional <Camera > cameraToDestroy ;
138136 @ Override protected void onPreMain () {
137+ cameraToDestroy = camera ;
139138 camera = Optional .absent ();
140139 }
141140
142141 @ Override protected Void onRunBackground () {
143142 if (cameraToDestroy .isPresent ()) {
144- stopPreview ();
145- cameraToDestroy .get ().release ();
143+ try {
144+ stopPreview ();
145+ cameraToDestroy .get ().release ();
146+ Log .w (TAG , "released old camera instance" );
147+ } catch (Exception e ) {
148+ Log .w (TAG , e );
149+ }
146150 }
147151 return null ;
148152 }
@@ -171,6 +175,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
171175 camera .get ());
172176 final Parameters parameters = camera .get ().getParameters ();
173177 if (preferredPreviewSize != null && !parameters .getPreviewSize ().equals (preferredPreviewSize )) {
178+ Log .w (TAG , "setting preview size to " + preferredPreviewSize .width + "x" + preferredPreviewSize .height );
174179 stopPreview ();
175180 parameters .setPreviewSize (preferredPreviewSize .width , preferredPreviewSize .height );
176181 camera .get ().setParameters (parameters );
@@ -239,7 +244,6 @@ public void flipCamera() {
239244 }
240245 }
241246
242-
243247 @ TargetApi (14 )
244248 private void onCameraReady () {
245249 if (!camera .isPresent ()) return ;
@@ -262,8 +266,8 @@ private void onCameraReady() {
262266 if (camera .isPresent ()) {
263267 try {
264268 camera .get ().setPreviewDisplay (surface .getHolder ());
265- startPreview ();
266- } catch (IOException e ) {
269+ requestLayout ();
270+ } catch (Exception e ) {
267271 Log .w (TAG , e );
268272 }
269273 }
@@ -273,13 +277,21 @@ private void onCameraReady() {
273277
274278 private void startPreview () {
275279 if (camera .isPresent ()) {
276- camera .get ().startPreview ();
280+ try {
281+ camera .get ().startPreview ();
282+ } catch (Exception e ) {
283+ Log .w (TAG , e );
284+ }
277285 }
278286 }
279287
280288 private void stopPreview () {
281289 if (camera .isPresent ()) {
282- camera .get ().stopPreview ();
290+ try {
291+ camera .get ().stopPreview ();
292+ } catch (Exception e ) {
293+ Log .w (TAG , e );
294+ }
283295 }
284296 }
285297
@@ -304,8 +316,7 @@ private void setCameraDisplayOrientation() {
304316 if (info .facing == Camera .CameraInfo .CAMERA_FACING_FRONT ) {
305317 displayOrientation = (info .orientation + degrees ) % 360 ;
306318 displayOrientation = (360 - displayOrientation ) % 360 ;
307- }
308- else {
319+ } else {
309320 displayOrientation = (info .orientation - degrees + 360 ) % 360 ;
310321 }
311322
@@ -401,7 +412,7 @@ public void onPreviewFrame(byte[] data, final Camera camera) {
401412 Log .w (TAG , "previewFormat: " + camera .getParameters ().getPreviewFormat ());
402413 Log .w (TAG , "croppingRect: " + croppingRect .toString ());
403414 Log .w (TAG , "rotation: " + rotation );
404- new RotatePreviewAsyncTask (previewSize , rotation , croppingRect ).execute (data );
415+ new CaptureTask (previewSize , rotation , croppingRect ).execute (data );
405416 }
406417 });
407418 }
@@ -430,7 +441,6 @@ private Rect getCroppedRect(Size cameraPreviewSize, Rect visibleRect, int rotati
430441 return visibleRect ;
431442 }
432443
433-
434444 @ SuppressWarnings ("SuspiciousNameCombination" )
435445 private void rotateRect (Rect rect ) {
436446 rect .set (rect .top , rect .left , rect .bottom , rect .right );
@@ -495,12 +505,12 @@ private abstract class PostInitializationTask<Result> extends SerialAsyncTask<Re
495505 }
496506 }
497507
498- private class RotatePreviewAsyncTask extends AsyncTask <byte [], Void , byte []> {
508+ private class CaptureTask extends AsyncTask <byte [], Void , byte []> {
499509 private final Size previewSize ;
500510 private final int rotation ;
501511 private final Rect croppingRect ;
502512
503- public RotatePreviewAsyncTask (Size previewSize , int rotation , Rect croppingRect ) {
513+ public CaptureTask (Size previewSize , int rotation , Rect croppingRect ) {
504514 this .previewSize = previewSize ;
505515 this .rotation = rotation ;
506516 this .croppingRect = croppingRect ;
@@ -516,6 +526,7 @@ protected byte[] doInBackground(byte[]... params) {
516526 rotation ,
517527 croppingRect );
518528 } catch (IOException e ) {
529+ Log .w (TAG , e );
519530 return null ;
520531 }
521532 }
0 commit comments