android saving 3 bitmaps as 1

2020-07-27 01:58发布

问题:

I've an app that takes a picture. It creates 2 subset bitmaps, processes these bitmaps with a distortion, then places the subsets over the original as overlays. Is there a way to save all three bitmaps as one (as it appears on the phone's screen)? I'd like to save what the user sees on the screen as a fourth bitmap on the sdcard? I've a feeling I've done this wrong.

Thanks

[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);
        }

回答1:

Yes it is possible to overlay multiple bitmaps and save as a single image file (PNG / JPEG / ..).

One way is to use Canvas class. Canvas along with Paint class defines the way in which bitmaps are overlayed.

Following code will demonstrate multiple bitmap overlay into a single image file.

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



回答2:

You can try the following. If you can, draw the images to a canvas:

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(...);


回答3:

You can use Bitma.compress(..) with FileOutputStream as last argument.

Read about external storage for instructions on how to write to sd-card.