简体   繁体   中英

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); and draw whatever it is you want to draw from CameraOverlay's onDraw() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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