How to overlay an image or view on top of a camera captured image

前端 未结 1 897
时光取名叫无心
时光取名叫无心 2021-02-05 19:30

What I am doing is drawing an image from the camera on to a canvas, then convert a view containing an image into a bitmap and draw it on t

相关标签:
1条回答
  • 2021-02-05 19:37

    Bitmaps retrieved from the camera are going to be massive (2000+ px wide). Your SurfaceView where you draw the image is only as large as your device resolution (~1000px wide). When you draw the one onto the other, you need to scale it, or it will end up 1/2 the size it should be (as in your screenshots). Here's how I'd modify your code.

    view.setDrawingCacheEnabled(true);
    viewCapture = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);
    //Source Rect sized to the surface view.
    Rect src = new Rect(0, 0, viewCapture.getWidth(), viewCapture.getHeight());
    //Destination RectF sized to the camera picture
    RectF dst = new RectF(0, 0, myImage.getWidth(), myImage.getHeight());
    canvas.drawBitmap(viewCapture, src, dst, paint);
    

    I know you make some effort to set the camera parameters and size, but either it's not working or you need to take a different approach. I think this should work, though. Your second screenshots, where the Android guy is clipped in both images suggest it's just a simple scaling problem.

    Hope that helps!

    0 讨论(0)
提交回复
热议问题