android 将 3 个位图保存为 1 个

发布于 2024-12-03 01:21:26 字数 664 浏览 1 评论 0原文

我有一个可以拍照的应用程序。它创建 2 个子集位图,对这些位图进行变形处理,然后将子集作为叠加层放置在原始位图上。有没有办法将所有三个位图保存为一个(如手机屏幕上显示的那样)?我想将用户在屏幕上看到的内容保存为 SD 卡上的第四个位图?我有一种感觉,我做错了。

谢谢

[更新1]

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拒绝两难 2024-12-10 01:21:26

是的,可以叠加多个位图并保存为单个图像文件(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());
}

沙什

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

情泪▽动烟 2024-12-10 01:21:26

您可以尝试以下操作。
如果可以的话,将图像绘制到画布上:

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

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(...);
九厘米的零° 2024-12-10 01:21:26

您可以使用 Bitma.compress(..)FileOutputStream 作为最后一个参数。

阅读外部存储,了解如何写入的说明SD 卡。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文