[英]Save image overlay with camera captured image underneith
我的应用程序具有“photobooth”功能,允许用户使用相机拍照,同时在相机视图顶部显示叠加图像。 拍照后,我需要保存用户在将图片带到文件系统时看到的内容。
我在开发解决方案时遇到了一个大问题:捕获具有兼容尺寸的图像,我可以在其中附加叠加图像,从而产生用户在拍照时看到的内容。
似乎我无法从具有定义尺寸的相机捕获图像(我必须从它们的列表中基本上选择)。 有些手机只能产生一定的尺寸。
由于我无法选择捕获图像的大小,似乎我需要包含许多不同大小的叠加图像,并将最佳匹配附加到捕获的图像。 我不能只是拍摄相机图像顶部的任何旧叠加层,使其看起来正确。
问题:
编辑:这是我的解决方案(简要)。 请注意这不是一个完美的,也许不是最有效的方法,但它确实有效。 有些事情可能是不必要/多余的,但无论如何
笔记:
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
}
}
}
我认为这是一个关于如何操纵叠加层的问题。 您可以根据捕获的图像大小裁剪它并调整大小以适应,保留其比例。 您可以通过将叠加比例与背景比率放置到最佳位置来放置叠加层。
我会保持足够大的覆盖层,带有宽边框(出血),可以使用过滤器轻松地将它们调整为图像,以便以良好的质量绘制它。 我认为叠加是你要设计并具有透明部分的东西,就像一个没有脸的小丑形象,这样用户就可以将某个人的脸掠过它?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.