在我的一个应用程序中,我需要进行屏蔽(将1个图像重叠到另一个图像)
在我的应用程序中,我必须将1个图像(位图)加载到imageview然后必须将一些fram应用于该图像我已经使用了另一个imageview ...这是完全正常工作
我的问题是...... 当我要保存位图时...... 使用这段代码
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, 0, 0, null);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmOverlay.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator
+ "test.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return bmOverlay;
}
我收到了
但我需要它像
答案 0 :(得分:1)
到目前为止,我发现的最好的是这一个:
public Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2.getHeight(), bmp1.getConfig());
float left =(bmp2.getWidth() - (bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight())))/(float)2.0;
float bmp1newW = bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight());
Bitmap bmp1new = getResizedBitmap(bmp1, bmp2.getHeight(), (int)bmp1newW);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1new, left ,0 , null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
答案 1 :(得分:0)
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
// determine the width of the canvas
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
//resize your bmp2
Bitmap resized = Bitmap.createScaledBitmap(bmp2, canvasWidth, canvasHeight, true);
// determine the centre of the canvas
int centreX = (canvasWidth - resized .getWidth()) /2;
int centreY = (canvasHeight - resized .getHeight()) /2
// This code can be used to alter the opacity of the image being overlayed.
//http://stackoverflow.com/a/12235235/1635441
//http://stackoverflow.com/a/5119093/1635441
//Paint p = new Paint();
//p.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP)); //http://stackoverflow.com/a/17553502/1635441
//p.setAlpha(180);
//p.setARGB(a, r, g, b);
//canvas.drawBitmap(resized, centreX, centreY, p);
//canvas.drawBitmap(bmp2, 0, 0, null);
canvas.drawBitmap(resized, centreX, centreY, null);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmOverlay.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator
+ "test.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return bmOverlay;
}