[英]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.