我有一个拍照的应用。它创建了2个子集位图,使用失真处理这些位图,然后将子集放在原始位置作为叠加层。有没有办法将所有三个位图保存为一个(如手机屏幕上显示的那样)?我想将用户在屏幕上看到的内容保存为SD卡上的第四个位图?我有一种感觉,我做错了。
由于
[UPDATE1]
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
Log.e(TAG, "******about to draw bgr ");
canvas.drawBitmap(bgr, 0, 0, null);
if (isLocked == true && bothCirclesInPlace == true){
if(overLay != null)
canvas.drawBitmap(overLay, centreX-radius, centreY-radius, null);
if(overLay2 != null)
canvas.drawBitmap(overLay2, centreA-radius, centreB-radius, null);
}
答案 0 :(得分:3)
是的,可以叠加多个位图并保存为单个图像文件(PNG / JPEG / ..)。
一种方法是使用Canvas类。 Canvas和Paint类定义了位图重叠的方式。
以下代码将演示多个位图叠加到单个图像文件中。
try {
// String mFilePath : Absolute Path of the file to be saved
// Bitmap mBitmap1 : First bitmap. This goes as background.
// Bitmap mCBitmap : Bitmap associated with the Canvas. All draws on the canvas are drawn into this bitmap.
// Bitmap mBitmap2 : Second bitmap. This goes on top of first (in this example serves as foreground.
// Paint mPaint1 : Paint to draw first bitmap
// Paint mPaint2 : Paint to draw second bitmap on top of first bitmap
Bitmap mCBitmap = Bitmap.createBitmap(mBitmap1.getWidth(), mBitmap1.getHeight(), mBitmap1.getConfig());
Canvas tCanvas = new Canvas(mCBitmap);
tCanvas.drawBitmap(mBitmap1, 0, 0, mPaint1);
mPaint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
// XFer modes define the overlay characteristic
tCanvas.drawBitmap(mBitmap2, 0, 0, mPaint2);
FileOutputStream stream = new FileOutputStream(mFilePath);
mCBitmap.compress(CompressFormat.JPEG, 100, stream);
stream.flush();
stream.close();
} catch(Exception e) {
Log.e("Could not save", e.toString());
}
词shash
答案 1 :(得分:1)
您可以尝试以下操作。 如果可以,将图像绘制到画布上:
Bitmap bitmap = new Bitmap(// Set the params you like //);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(// The first picture //);
canvas.drawBitmap(// First overlay //);
canvas.drawBitmap(// Second overlay //);
// You can draw whatever you like
// For example:
canvas.drawRectangle(...);
// Now the bitmap can will hold everything you draw on the canvas.
// You can save the bitmap to the SD
bitmap.compress(...);
答案 2 :(得分:0)
您可以将Bitma.compress(..)与FileOutputStream一起用作最后一个参数。
了解有关如何写入SD卡的说明,请阅读external storage。