4.1中未调用Android Camera PreviewCallback

时间:2012-12-07 22:48:19

标签: android video callback camera frame

我有一个应用程序来获取带有曲面的相机预览帧。它适用于Android 4.0.4,但在更新后它不适用于同一设备上的Jelly Bean,4.1.2。简单地说,回调永远不会被回调。这是代码:剪了一下:

public class Panel extends Activity {
    Camera myCamera;
    int cameraId = -1;
    MyCameraSurfaceView myCameraSurfaceView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_panel);

        myCamera = getCameraInstance();
        myCamera.setPreviewCallback(new Camera.PreviewCallback() {
            @Override
            public void onPreviewFrame(byte[] data, Camera camera) {
                Log.d("Camera Preview", data.length + "");
            }
        });

        myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
        FrameLayout myCameraPreview = (FrameLayout) findViewById(R.id.videoview);
        myCameraPreview.addView(myCameraSurfaceView);
    }

    private Camera getCameraInstance() {
        Camera c = null;
        try {
            c = Camera.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }


    public class MyCameraSurfaceView extends SurfaceView implements
            SurfaceHolder.Callback {

        private SurfaceHolder mHolder;
        private Camera mCamera;

        public MyCameraSurfaceView(Context context, Camera camera) {
            super(context);
            mCamera = camera;
            mHolder = getHolder();
            mHolder.addCallback(this);
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            } catch (IOException e) {
            }
        }
    }
}

视频帧正在活动中显示,所以我无法弄清楚我做错了什么。

1 个答案:

答案 0 :(得分:4)

我已经使用另一个教程重写了代码。它有效,但现在变慢了。我不知道确切的原因(这可能是由于我使用的泄漏的N7000 ROM,它可能有一个错误或者在4.1中实现的方式不同,不确定)

CameraPreview.java

public class CameraPreview implements SurfaceHolder.Callback,
        Camera.PreviewCallback {
    int PreviewSizeWidth;
    int PreviewSizeHeight;
    SurfaceHolder mSurfHolder;
    Camera mCamera;

    public CameraPreview(int PreviewlayoutWidth, int PreviewlayoutHeight) {
        PreviewSizeWidth = PreviewlayoutWidth;
        PreviewSizeHeight = PreviewlayoutHeight;
    }



    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        Parameters p = camera.getParameters();  
        int width = p.getPreviewSize().width;
        int height = p.getPreviewSize().height;

        ByteArrayOutputStream outstr = new ByteArrayOutputStream();
        Rect rect = new Rect(0, 0, width, height);
        YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, width,
                height, null);
        yuvimage.compressToJpeg(rect, 80, outstr); // outstr contains image in jpeg  
        String encodedImage = Base64.encodeToString(
                outstr.toByteArray(), Base64.DEFAULT); // this is base64 encoding of image


    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        Parameters parameters;
        mSurfHolder = arg0;

        parameters = mCamera.getParameters();
        parameters.setPreviewSize(PreviewSizeWidth, PreviewSizeHeight);

        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

    public void surfaceCreated(SurfaceHolder arg0) {
        mCamera = Camera.open();
        try {
            // If did not set the SurfaceHolder, the preview area will be black.
            mCamera.setPreviewDisplay(arg0);
            mCamera.setPreviewCallback(this);
            Parameters p = mCamera.getParameters();
            p.setPreviewSize(PreviewSizeWidth, PreviewSizeHeight);
            mCamera.setParameters(p);
        } catch (IOException e) {
            mCamera.release();
            mCamera = null;
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        mCamera.setPreviewCallback(null);
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }
}

PanelActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_panel);


    SurfaceView camView = new SurfaceView(this);
    SurfaceHolder camHolder = camView.getHolder();
    int width = 352; // must set a compatible value, otherwise it gets the default width and height
    int height = 288;

    camPreview = new CameraPreview(width, height);

    camHolder.addCallback(camPreview);
    camHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    mainLayout = (FrameLayout) findViewById(R.id.videoview);
    mainLayout.addView(camView, new LayoutParams(width, height));

}