如何在相机拍摄图像的顶部覆盖图像或视图 [英] How to overlay an image or view on top of a camera captured image

查看:209
本文介绍了如何在相机拍摄图像的顶部覆盖图像或视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做的是将相机中的图像绘制到画布,然后转换包含视图将图像转换为位图并将其绘制在画布顶部。然后,最终的位图将保存到外部存储。

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. Then the final bitmap gets saved to the external storage.

问题是 canvas 似乎正在调整为预览窗口大小,而不是保存的图像大小。

the problem is the canvas appears to be sizing itself to the preview window size, instead of the saved image size.

这是我的活动类中的一部分将捕获的视图合并在摄像机图像之上:

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

  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:

此图像显示预览窗口。正如你可以看到,Android是在图像的死中间。

This image shows the preview windows. As you can see the Android is in the dead middle of the image.

I捕获上面的图像,并保存。我去查看它,这就是它的样子:

I capture the image above and it gets saved. I go to view it and this is what it looks like:

请注意,Android不再在中间,它位于左上角。

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

推荐答案

从相机检索的位图将是巨大的(2000+ px宽)。您绘制图像的SurfaceView只有您的设备分辨率(〜1000像素宽)才会很大。当你把它放在另一个上时,你需要对它进行缩放,否则它将会被缩小到1/2的大小(如你的截图)。以下是我修改代码的方式。

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

我知道你做了一些努力来设置相机参数和尺寸,但是它不工作或者你需要采取不同的方法。我认为这应该是有效的。您的第二个屏幕截图,Android人物在两个图像中都被剪辑,建议它只是一个简单的缩放问题。

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.

希望有帮助!

这篇关于如何在相机拍摄图像的顶部覆盖图像或视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆