如何叠加图像或查看照相机拍摄的图像的顶 [英] How to overlay an image or view on top of a camera captured image

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

问题描述

包含我在做什么上绘制从相机的图像到画布,然后转换视图图像插入位图,并绘制在画布上。然后最后位图被保存到外部存储。

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.

问题是画布看来是大小,而不是保存的图像尺寸本身的preVIEW窗口大小。

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.

我通过以下操作在我surfaceChanged设置不同尺寸

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

显然,这样一来,我得到了preVIEW窗口中的最佳分辨率,并且拍摄的图像的最佳分辨率。我认为问题是由两种不同的分辨率,但弄清楚为什么就是我坚持。

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:

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

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

我拍摄上面的图片,它被保存。我去查看它,这就是它的样子:

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部分离开屏幕,如图中的下一个摄像机preVIEW

As another test, I moved the android part way off the screen as shown in the next camera preview

我捕捉到的图像完全如上图所示,并在观看它时,Android是切断它究竟是怎么回事在preVIEW窗口,但其又在左上角,当它应该是全屏幕

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+像素宽)。您SurfaceView,你画的图像只是大如您的设备分辨率(〜1000px宽)。当你画一个到另一个,你需要扩展它,或者它最终将1/2应该是大小(如在你的屏幕截图)。下面是我如何修改code。

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天全站免登陆