目前我在相机表面视图上有一个上叠加图像。
我想做的只是输出与上层重叠的照片部分。到目前为止,我可以合并两个图像,这意味着重叠层位于摄像机视图的顶部。但是我怎样才能获得叠加部分输出?感谢
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap resizedBottom;
Bitmap resizedTop;
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap photo=BitmapFactory.decodeByteArray(data, 0, data.length,options);
Matrix matrix = new Matrix();
matrix.postRotate(passDegree);
Bitmap rotatedBitmap = Bitmap.createBitmap(photo, 0, 0,photo.getWidth(), photo.getHeight(), matrix, true);
photo.recycle();
photo = null;
if (h >= w) {
h = w;
} else {
w = h;
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
resizedBottom = Bitmap.createScaledBitmap(rotatedBitmap,w,h,false);
resizedTop = Bitmap.createScaledBitmap(topLayer,w,h,false);
} else {
resizedBottom = Bitmap.createScaledBitmap(rotatedBitmap,h,w,false);
resizedTop = Bitmap.createScaledBitmap(topLayer,h,w,false);
}
Canvas c = new Canvas(resizedBottom);
Resources res = getResources();
Drawable drawable1 = new BitmapDrawable(res,resizedBottom);
Drawable drawable2 = new BitmapDrawable(res,resizedTop);
if (res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
drawable1.setBounds(0, 0, w, h);
drawable2.setBounds(0, 0, w, h);
} else {
drawable1.setBounds(0, 0, h, w);
drawable2.setBounds(0, 0, h, w);
}
drawable1.draw(c);
drawable2.draw(c);
File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
// Write to SD Card
String fileName = String.format(sdDir+"/%d.jpg", System.currentTimeMillis());
OutputStream os = null;
try {
os = new FileOutputStream(fileName);
resizedBottom.compress(Bitmap.CompressFormat.PNG, 50, os);
} catch(IOException e) {
e.printStackTrace();
}
Log.i("test", "onPictureTaken - jpeg");
resetCam();
}
答案 0 :(得分:3)
花了一个下午,最后有解决方案,结果比我想象的更容易:
Just
1.Create a temp bitmap to store render result
2.Set PorterDuff.Mode.DST_IN on top layer(In this mode only the overlap area is drawn)
示例代码:
Bitmap result = Bitmap.createBitmap(topLayer.getWidth(), topLayer.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(resizedBottom, 0, 0, null);
mCanvas.drawBitmap(topLayer, 0, 0, paint);
paint.setXfermode(null);
进一步阅读PorterDuff模式: http://www.ibm.com/developerworks/java/library/j-mer0918/