PreviewCallback and PreviewCallback with a buffer are not called

I have a question regarding preview callbacks with Android 4.0.x. I installed the camera, create a surface to display the camera image on previewCallback -event. Everything is working fine.

But with Android 4.0.x, neither onPreviewCallback is called, nor onPreviewCallbackWithBuffer . Is there a workaround for this problem?

I want to take a screenshot and donโ€™t want to use takePicture() -way because it freezes the live image for a short time.

+6
source share
2 answers

You must call setPreviewCallback in the surfaceChanged method, and not just in surfaceCreated.

 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { if (mHolder.getSurface() == null){ return; } try { mCamera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } try { mCamera.setPreviewCallback(this); mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (Exception e){ Log.d(TAG, "Error starting camera preview: " + e.getMessage()); } } 
+7
source

you can call setOneShotPreviewCallback

 mCamera.setOneShotPreviewCallback(new Camera.PreviewCallback() { @Override public void onPreviewFrame(byte[] data, Camera camera) { Camera.Parameters parameters = camera.getParameters(); int format = parameters.getPreviewFormat(); //YUV formats require more conversion if (format == ImageFormat.NV21 || format == ImageFormat.YUY2 || format == ImageFormat.NV16) { int w = parameters.getPreviewSize().width; int h = parameters.getPreviewSize().height; // Get the YuV image YuvImage yuv_image = new YuvImage(data, format, w, h, null); // Convert YuV to Jpeg Rect rect = new Rect(0, 0, w, h); ByteArrayOutputStream output_stream = new ByteArrayOutputStream(); yuv_image.compressToJpeg(rect, 100, output_stream); byte[] byt = output_stream.toByteArray(); FileOutputStream outStream = null; try { // Write to SD Card File file = createFileInSDCard(FOLDER_PATH, "Image_"+System.currentTimeMillis()+".jpg"); //Uri uriSavedImage = Uri.fromFile(file); outStream = new FileOutputStream(file); outStream.write(byt); outStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } } } 
+6
source

All Articles