简体   繁体   English

如何在Camera Preview上绘制

[英]How to draw on Camera Preview

for some time I've been working on this app, that allows user to take photos. 我从事此应用程序已有一段时间了,该应用程序允许用户拍照。 The interesting thing about it, is that there should be some lines drawn over camera preview. 有趣的是,应该在摄像机预览上画一些线。 The problem is that I'm not quite sure how to do this. 问题是我不太确定如何执行此操作。 I do have codes to initiate camera and to draw line, but I do not know how to merge them. 我确实有启动照相机和画线的代码,但是我不知道如何合并它们。

I use this code to initiate camera on button click: 我使用以下代码在单击按钮时启动摄像头:

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



        }
    });

And here's how I usually draw lines: 这是我通常画线的方式:

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

You will need an overlay to draw on top of the live camera preview. 您将需要一个叠加层才能在实时摄像机预览的顶部绘制。 The overlay is a child class of the surface the preview is drawn on. 叠加层是在其上绘制预览的曲面的子类。 The preview class looks something like this: 预览类如下所示:

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)


         }

From your main activity you would call: Preview CameraPreview = new Preview(this); 从您的主要活动中,您将调用: Preview CameraPreview = new Preview(this); and draw whatever it is you want to draw from CameraOverlay's onDraw() . 并从CameraOverlay的onDraw()绘制任何要绘制的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM