5

我有一个应用程序来获取带有表面的相机预览帧。它在 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) {
            }
        }
    }
}

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

4

1 回答 1

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));

}
于 2013-01-15T21:15:45.823 回答