简体   繁体   English

保存图像覆盖与相机捕获图像underneith

[英]Save image overlay with camera captured image underneith

My application has a "photobooth" feature which will allow the user to take a picture with the camera and at the same time show an overlay image on top of the camera view. 我的应用程序具有“photobooth”功能,允许用户使用相机拍照,同时在相机视图顶部显示叠加图像。 After the picture is taken, i need to save what the user saw while taking the picture to the filesystem. 拍照后,我需要保存用户在将图片带到文件系统时看到的内容。

I have experienced 1 big problem while developing a solution to this: capturing an image with the compatible dimensions in which i can attach an overlay image to resulting in what the user saw while taking the picture. 我在开发解决方案时遇到了一个大问题:捕获具有兼容尺寸的图像,我可以在其中附加叠加图像,从而产生用户在拍照时看到的内容。

It seems i cannot capture an image from the camera with defined dimensions(i have to basically pick from a list of them). 似乎我无法从具有定义尺寸的相机捕获图像(我必须从它们的列表中基本上选择)。 Some phones only can produce certain dimensions. 有些手机只能产生一定的尺寸。

Since i cannot choose the size of the captured image, it seems as though i will be required to include many different sizes of the overlay image, and attach the best match to the captured image. 由于我无法选择捕获图像的大小,似乎我需要包含许多不同大小的叠加图像,并将最佳匹配附加到捕获的图像。 I can't just slap any old overlay on top of the camera image and make it look right. 我不能只是拍摄相机图像顶部的任何旧叠加层,使其看起来正确。

Questions: 问题:

  • Am i over-complicating this "camera image + overlay image creation" process? 我是否过度复杂化了“相机图像+叠加图像创建”过程?
  • What suggestions do you have in completing this task without the need of including several different sizes overlay images? 在完成此任务时您有什么建议,而不需要包含多个不同大小的叠加图像?

Edit: Here is my solution(brief). 编辑:这是我的解决方案(简要)。 Please realize this is not a perfect and maybe not most efficient way to do this, but it works. 请注意这不是一个完美的,也许不是最有效的方法,但它确实有效。 Some things may be unnecessary/redundant but whatever! 有些事情可能是不必要/多余的,但无论如何

Notes: 笔记:

  • this doesn't work too great on tablet devices. 这对平板电脑设备来说效果不是很好。
  • the overlay image needs to be rotated to be in landscape mode(even though you will be taking the image holding the phone in portrait) 需要将叠加图像旋转到横向模式(即使您将拍摄手持图像的图像)
  • overlay size is 480x320 叠加尺寸为480x320
  • you need to force the activity to landscape mode while taking the picture(now the overlay looks like its portrait!) 您需要在拍摄照片时强制将活动设置为横向模式(现在叠加层看起来像它的肖像!)
  • i add the overlay image view using addContentView(overlayImageView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 我使用addContentView(overlayImageView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));添加叠加图像视图addContentView(overlayImageView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

... ...

final Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap mutableBitmap = null;
        try {
                    //for a PORTRAIT overlay and taking the image holding the phone in PORTRAIT mode
            mutableBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options).copy(Bitmap.Config.RGB_565, true);
            Matrix matrix = new Matrix();
            int width = mutableBitmap.getWidth();
            int height = mutableBitmap.getHeight();
            int newWidth = overlayImage.getDrawable().getBounds().width();
            int newHeight = overlayImage.getDrawable().getBounds().height();
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            matrix.postScale(scaleWidth, scaleHeight);
            matrix.postRotate(90);

            Bitmap resizedBitmap = Bitmap.createBitmap(mutableBitmap, 0, 0, mutableBitmap.getWidth(), mutableBitmap.getHeight(), matrix, true);
            finalBitmap = resizedBitmap.copy(Bitmap.Config.RGB_565, true);
            Canvas canvas = new Canvas(finalBitmap);

            Bitmap overlayBitmap = BitmapFactory.decodeResource(getResources(), overlay);
            matrix = new Matrix();
            matrix.postRotate(90);
            Bitmap resizedOverlay = Bitmap.createBitmap(overlayBitmap, 0, 0, overlayBitmap.getWidth(), overlayBitmap.getHeight(), matrix, true);
            canvas.drawBitmap(resizedOverlay, 0, 0, new Paint());
            canvas.scale(50, 0);
            canvas.save();
            //finalBitmap is the image with the overlay on it
        }
        catch(OutOfMemoryError e) {
            //fail
        }
    }
}

I think this is a question of how you manipulate your overlays. 我认为这是一个关于如何操纵叠加层的问题。 You can crop it according to the captured image size and resize it to fit, preserving its ratio. 您可以根据捕获的图像大小裁剪它并调整大小以适应,保留其比例。 You can place the overlay, by comparing its ratio to the backround ratio, to its optimal position. 您可以通过将叠加比例与背景比率放置到最佳位置来放置叠加层。

I would keep overlays big enough, with a wide border (bleed), to easily size them to an image using filters to draw it with good qaulity. 我会保持足够大的覆盖层,带有宽边框(出血),可以使用过滤器轻松地将它们调整为图像,以便以良好的质量绘制它。 I guess overlays are something which you would design and have transparent parts, like an image of a clown without a face so the user can snap somebody elses face into it? 我认为叠加是你要设计并具有透明部分的东西,就像一个没有脸的小丑形象,这样用户就可以将某个人的脸掠过它?

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

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