我正在尝试从相机捕获图片并将徽标徽标覆盖在该图片的左上角。我尝试了使用Canvas进行叠加的方法,但是它不起作用。这是我的代码:
此方法用于将图片保存到存储器中
public void saveBitmapToGallery(Bitmap bm, String picturename) {
String root = Environment.getExternalStorageDirectory().toString();
File mydir = new File(picturepath);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayheight = dm.heightPixels;
displaywidth = dm.widthPixels;
File file = new File(mydir, picturename + ".JPG");
try {
FileOutputStream fos = new FileOutputStream(file);
Bitmap.createScaledBitmap(bm, displaywidth, displayheight, true);
bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这是我写的在图片上叠加位图的方法:
public Bitmap embedBitmap(Bitmap src){ // This method receives the bitmap from the OnActivityResult method
int bitmapwidth = src.getWidth();
int bitmapheight = src.getHeight();
int logowidth = logo.getWidth();
int logoheight = logo.getHeight();
float marginLeft = (float) (bitmapwidth * 0.5 - logowidth * 0.5);
float marginTop = (float) (bitmapheight * 0.5 - logoheight * 0.5);
//Bitmap dest = Bitmap.createBitmap(bitmapwidth,bitmapheight,src.getConfig());
Canvas cv = new Canvas(src);
//cv.drawBitmap(src,new Matrix(),null);
cv.drawBitmap(logo, 0,0,new Paint());
saveBitmapToGallery(src, picturename);
return src;
}
}
让我知道需要做什么。谢谢。