Camera.PreviewCallback没有回调

时间:2013-09-09 05:45:09

标签: callback camera

(版本:android 4.1)我想在相机开始预览时添加一些效果。但是onPreviewFrame函数是一样的 没有打过电话。我知道如果使用setdisplaypreview()方法将调用onPreviewFrame。 但是我无法为预览添加效果。

所以请告诉我如何使它有效。谢谢你的帮助。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSurface = (SurfaceView) findViewById(R.id.surfaceview);
    //SurfaceView surfaceview = new SurfaceView(this);
    mHolder = mSurface.getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
     initCamera();
}

void openDevice() {
    if (mCamera == null) {
        mCamera = Camera.open();
    }
}

private void initCamera()  {
    openDevice();
    Camera.Parameters parameter = mCamera.getParameters();
    parameter.setPictureSize(PREVIEW_WIDTH, PREVIEW_HEIGHT);
    parameter.setPreviewFormat(ImageFormat.JPEG);
    mCamera.setParameters(parameter);
    mCamera.setDisplayOrientation(90);
}

private void drawBitmap(Bitmap map) {

    Canvas canvas = mHolder.lockCanvas();
    if (null != canvas) {
        Paint paint = new Paint();
        if (null != canvas) {
            canvas.drawBitmap(map, null, mDestRect, paint);
            mSurface.getHolder().unlockCanvasAndPost(canvas);
        }
        mHolder.unlockCanvasAndPost(canvas);
    }
}
    @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    if (holder.getSurface() == null) {
        return;
    }
    mHolder = holder;
    if (mCamera == null) {
        return;
    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if( null != mCamera){

        mCamera.setPreviewCallback(mPreviewCallBack);
        mCamera.startPreview();
    }
}
    Camera.PreviewCallback mPreviewCallBack = new Camera.PreviewCallback() {

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        int format = camera.getParameters().getPreviewFormat();
        Size s = camera.getParameters().getPreviewSize();

        Log.e(TAG, "xxxxxxxxxx");
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        try {
            byteStream.write(data);
            //TODO I have add some effect here.

            mOutBitmap
                    .compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
            byteStream.close();
                           //Draw the preview image.
            drawBitmap(mOutBitmap);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
};

1 个答案:

答案 0 :(得分:1)

在initCamera()& onSurfaceCreated()方法,使用下面的代码并查看输出: -

byte[] mVideoSource;

public void initCamera(){
    openDevice();
    Camera.Parameters parameter = mCamera.getParameters();

    PixelFormat lPixelFormat = new PixelFormat();
    PixelFormat.getPixelFormatInfo(parameter.getPreviewFormat(), lPixelFormat);
    int lSourceSize = PREVIEW_WIDTH * PREVIEW_HEIGHT * lPixelFormat.bitsPerPixel / 8;
    mVideoSource = new byte[lSourceSize];

    parameter.setPreviewFormat(PixelFormat.YCbCr_420_SP);
    parameter.setPictureSize(PREVIEW_WIDTH, PREVIEW_HEIGHT);
    parameter.setPreviewSize(PREVIEW_WIDTH, PREVIEW_HEIGHT);

    mCamera.setParameters(parameter);
    mCamera.setDisplayOrientation(90);
    mCamera.addCallbackBuffer(mVideoSource);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if( null != mCamera){
    mCamera.setPreviewDisplay(mHolder);
    mCamera.addCallbackBuffer(mVideoSource);
    mCamera.setPreviewCallbackWithBuffer(previewCallback);
    mCamera.startPreview();
}