0

(版本: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();
        }
    }
};
4

1 回答 1

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();
}
于 2013-09-11T13:18:51.140 回答