简体   繁体   English

如何在相机捕获的图像上叠加图像或视图

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

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 top of the canvas. 我正在做的是将相机上的图像绘制到canvas ,然后将包含图像的view转换为bitmap并将其绘制在画布上。 Then the final bitmap gets saved to the external storage. 然后将最终的bitmap保存到外部存储器。

the problem is the canvas appears to be sizing itself to the preview window size, instead of the saved image size. 问题是canvas似乎是将自身大小调整为预览窗口大小,而不是保存的图像大小。

Here is the part in my activity class that is merging the captured view on top of the camera image: 以下是我的活动类中的部分,它将捕获的视图合并到摄像机图像的顶部:

        Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                imageData.length, options);
        Bitmap mutableBitmap = myImage.copy(Bitmap.Config.ARGB_8888, true);

        Paint paint = new Paint();
        Canvas canvas = new Canvas(mutableBitmap);

        view.setDrawingCacheEnabled(true);
        viewCapture = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        canvas.drawBitmap(viewCapture, 0, 0, paint);

        fileOutputStream = new FileOutputStream(dir + "/" + imgName + ".jpg");

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
        mutableBitmap.compress(CompressFormat.JPEG, quality, bos);

This saves the bitmap on top of the captured image, but due to the different sizes, its offset into the top left hand corner. 这会将位图保存在捕获的图像之上,但由于尺寸不同,它会偏移到左上角。

I'm setting the different sizes by doing the following in my surfaceChanged 我通过在surfaceChanged中执行以下操作来设置不同的大小

  Camera.Parameters p = mCamera.getParameters();
  Camera.Size myBestPreviewSize = getBestPreviewSize(w, h, p);
  p.setPreviewSize(myBestPreviewSize.width, myBestPreviewSize.height);
  Camera.Size myBestPictureSize = getBestPictureSize(w, h, p);
  p.setPictureSize(myBestPictureSize.width, myBestPictureSize.height);

Obviously by doing this, I am getting the best resolution for the preview window, and the best resolution for the captured image. 显然,通过这样做,我获得了预览窗口的最佳分辨率,以及捕获图像的最佳分辨率。 I believe the issue is caused by the two different resolutions, but figuring out why is where I'm stuck. 我认为这个问题是由两个不同的决议引起的,但弄清楚为什么我会陷入困境。

Hopefully the following images will better describe the issue: 希望以下图片能更好地描述问题:

This image shows the preview windows. 此图像显示预览窗口。 As you can see the Android is in the dead middle of the image. 正如您所看到的,Android处于图像的中间位置。 在此输入图像描述

I capture the image above and it gets saved. 我捕获上面的图像并保存。 I go to view it and this is what it looks like: 我去看它,这就是它的样子: 在此输入图像描述

Notice the Android is no longer in the middle, its in the top left corner. 请注意,Android不再位于中间位置,位于左上角。

As another test, I moved the android part way off the screen as shown in the next camera preview 作为另一个测试,我将android部分移出屏幕,如下一个相机预览所示 在此输入图像描述

I capture the image exactly as shown above, and upon viewing it, the android is cut off exactly how it was in the preview window, but again its in the top left hand corner when it should be full screen 我完全按照上面所示捕获图像,并且在查看它时,android完全切断了它在预览窗口中的状态,但是当它应该是全屏时它再次位于左上角 在此输入图像描述

Bitmaps retrieved from the camera are going to be massive (2000+ px wide). 从相机中检索到的位图将是巨大的(2000 + px宽)。 Your SurfaceView where you draw the image is only as large as your device resolution (~1000px wide). 绘制图像的SurfaceView仅与设备分辨率(~1000px宽)一样大。 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). 当你将它绘制到另一个上时,你需要缩放它,或者它最终应该是它应该的大小的1/2(如截图中所示)。 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. 你的第二个屏幕截图,其中两个图像中的Android人物剪辑表明它只是一个简单的缩放问题。

Hope that helps! 希望有所帮助!

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

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