1

我的应用程序当前有一个预览屏幕,我希望它每秒捕获许多帧进行处理。目前,我的预览每秒只存储一张图像,但是我需要更大的 fps 捕获率。任何帮助,将不胜感激。

另一个问题(如果可以的话)是我的图像出现在我的 SD 卡上时旋转了 90 度。到目前为止,没有任何互联网解决方案可以帮助我解决这些问题:(

谢谢 :)

    public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{

    private SurfaceHolder mHolder;
    private Camera mCamera;

    public MyCameraSurfaceView(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int weight,
                               int height) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.
        mCamera.setDisplayOrientation(90);

        if (mHolder.getSurface() == null){
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
            // ignore: tried to stop a non-existent preview
        }

        // make any resize, rotate or reformatting changes here

        // start preview with new settings
        try {

            mCamera.setPreviewDisplay(mHolder);
            Camera.Parameters parameters = mCamera.getParameters();
            List<Camera.Size> size = parameters.getSupportedPreviewSizes();
            parameters.setPreviewSize(size.get(0).width, size.get(0).height);
            mCamera.setParameters(parameters);
            mCamera.startPreview();

        } catch (Exception e){}

        mCamera.setPreviewCallback(new Camera.PreviewCallback() {

            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, 10, output_stream);
                    byte[] byt = output_stream.toByteArray();

                    FileOutputStream outStream = null;
                    try {
                        outStream = new FileOutputStream(String.format(
                                "/sdcard/bb%d.jpg", System.currentTimeMillis() / 1000));

                        outStream.write(byt);
                        outStream.close();


                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {

                    }

                }

            }
        });

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setDisplayOrientation(90);
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();


        } catch (IOException e) {
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

}
4

2 回答 2

0

setPreviewCallback()是一种请求预览帧的简单但效率较低的方法。主要问题是框架可能非常忙于分配byte[]要填充的块,而垃圾收集器可能会付出沉重的代价。首选方法是使用setPreviewCallbackWithBuffer(),但即使这样也不能保证所需的帧速率,如如何在 Android 中以每秒至少 15 帧的速度从 Camera 对象获取原始预览数据?

于 2014-01-02T14:05:32.183 回答
0

首先,请注意Camera.Parameters允许您使用 设置帧速率setPreviewFrameRate。该值必须在 描述的范围内getPreviewFpsRange

其次,在 a 中处理预览帧byte[]将严重限制您的帧速率,因为必须复制大量数据。如果您想将未修改的全帧 YUV 数据写入磁盘,那么您目前别无选择。如果您可以处理压缩伪影,并且您拥有 Android 4.3 或更高版本,则可以将数据保存为 MPEG 视频并稍后读取帧。有关代码示例,请参阅此页面上的 CameraToMpegTest.java 示例。

将图像旋转 90 或 180 度很容易编码。Bitmap如果你不想自己写,班级可以这样做。

于 2013-08-27T20:21:30.163 回答