[英]How to draw on Camera Preview
我从事此应用程序已有一段时间了,该应用程序允许用户拍照。 有趣的是,应该在摄像机预览上画一些线。 问题是我不太确定如何执行此操作。 我确实有启动照相机和画线的代码,但是我不知道如何合并它们。
我使用以下代码在单击按钮时启动摄像头:
initCamera.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
/*Intent nextScreen = new Intent(getActivity().getApplicationContext(), CameraActivity.class);
startActivity(nextScreen);*/
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
} catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
这是我通常画线的方式:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
imageView1 = (ImageView) findViewById(R.id.imageView1);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
bmp = Bitmap.createBitmap(width, height, conf);
canvas = new Canvas(bmp);
p = new Paint();
p.setColor(Color.RED);
imageView1.setImageBitmap(bmp);
p.setStrokeWidth(5);
canvas.drawLine(0, height/2, width, height/2, p);
您将需要一个叠加层才能在实时摄像机预览的顶部绘制。 叠加层是在其上绘制预览的曲面的子类。 预览类如下所示:
class Preview extends ViewGroup implements SurfaceHolder.Callback, Camera.PreviewCallback
{
Preview(Context context)
{
super(context);
SurfaceView mSurfaceView;
SurfaceHolder mHolder;
mSurfaceView = new SurfaceView(PreviewContext);
addView(mSurfaceView);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
PreviewCameraOverlay = new CameraOverlay(context);
addView(PreviewCameraOverlay);
}
// Additional functions like:
// surfaceChanged(SurfaceHolder holder, int format, int width, int height)
// onPreviewFrame(byte[] data, Camera pCamera)
// etc.
}
protected class CameraOverlay extends View
{
public CameraOverlay(Context context)
{
super(context);
setWillNotDraw(false);
setBackgroundColor(Color.TRANSPARENT);
setAlpha(1f);
OverlayPaint = new Paint[PaintColors];
}
// Additional functions:
// onDraw(Canvas canvas)
// onMeasure(int widthMeasureSpec, int heightMeasureSpec)
}
从您的主要活动中,您将调用: Preview CameraPreview = new Preview(this);
并从CameraOverlay的onDraw()
绘制任何要绘制的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.