PreviewCallback and PreviewCallback with buffer ar

2019-04-28 05:49发布

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

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

I want to take a screen shot and do not want to use the takePicture()-way because it freezes the live image for a short-time.

2条回答
聊天终结者
2楼-- · 2019-04-28 06:11

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 {
                        }
                    }
                }
查看更多
Root(大扎)
3楼-- · 2019-04-28 06:37

You must to call setPreviewCallback in surfaceChanged method, not only 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());
    }
}
查看更多
登录 后发表回答