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