如何绘制相机预览

时间:2015-11-30 15:50:07

标签: java android android-camera android-canvas android-camera-intent

有一段时间我一直在研究这个应用程序,它允许用户拍照。关于它的有趣之处在于,应该在相机预览上绘制一些线条。问题是我不太清楚如何做到这一点。我确实有代码来启动相机和画线,但我不知道如何合并它们。

我使用此代码在按钮点击时启动相机:

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

1 个答案:

答案 0 :(得分:0)

您需要在实时相机预览之上绘制叠加层。叠加层是绘制预览的曲面的子类。预览类看起来像这样:

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()中绘制的任何内容。