简体   繁体   English

合并相机预览与图像?

[英]Merge Camera preview with a image?

I am trying to make a composite image from a camera preview and an ImageView that was above it. 我正在尝试从相机预览和上面的ImageView制作合成图像。 I have one image that is a transparent png which is set on an imageview like this 我有一个透明的png图像,它在像这样的imageview上设置

ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.one);

I then add it to the framelayout which is already showing my camera preview (inherits SurfaceView) like so: 然后我将它添加到framelayout,它已经显示我的相机预览(继承SurfaceView),如下所示:

preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(cp); //cp is a reference to a camera preview object
preview.addView(iv);

My imageview's picture is this: 我的imageview的图片是这样的:

在此输入图像描述

And the screen is something like this (I had to take a the pic from another camera since the DDMS screenshot wasn't showing the preview only the image and a black screen, don't know if that's a relevant though): 屏幕是这样的(我不得不从另一台相机拍摄照片,因为DDMS截图没有显示预览图像和黑屏,不知道这是否相关):

在此输入图像描述

Now my task is to take that picture with the imageview. 现在我的任务是用imageview拍摄那张照片。 I came up with two approaches, both of which I do not know whether they can or cannot be implemented 我想出了两种方法,我不知道它们是否可以实现

  1. Save the picture seperately, keep track of which cover was on the image and then merge someway. 单独保存图片,跟踪图像上的哪个封面然后合并。 Can this be done, and how? 可以这样做,怎么做?
  2. Gain the look of the framelayout in which both Views are residing and save as image 获取两个视图所在的framelayout的外观并保存为图像
  3. Take screenshot of a specific area, I will onky do this one as a last resort ie if this can be done 截取特定区域的屏幕截图,我将onky做这个作为最后的手段,即如果可以做到这一点

What I want to know is which one these approaches is possible and how can it be done? 我想知道的是这些方法可以采用哪种方法以及如何实现? or is there a better way to get this done? 还是有更好的方法来完成这项工作?

Assuming you already have image in form of byte[] data from jpeg callback. 假设你已经有来自jpeg回调的byte[] data形式的图像。

  1. Decode the image into a mutable bitmap: 将图像解码为可变位图:

     Bitmap photo = BitmapFactory.decodeByteArray(data, 0, data.length); photo = photo.copy(photo.getConfig(), true); 
  2. Read the overlay: 阅读叠加层:

     Bitmap overlay = BitmapFactory.decodeResource(getResources(), R.drawable.one); 
  3. Draw overlay on the photo: 在照片上绘制叠加层:

     Canvas canvas = new Canvas(photo); canvas.drawBitmap(overlay, new Matrix(), null); 

Now, photo should contain your image. 现在, photo应该包含您的图像。

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

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