【问题标题】:android saving 3 bitmaps as 1android将3个位图保存为1
【发布时间】:2011-09-04 16:39:59
【问题描述】:

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

谢谢

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

【问题讨论】:

    标签: java android image-processing bitmap


    【解决方案1】:

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

    沙士

    【讨论】:

    • 嗨,谢谢你的代码。我可以问一个问题吗?在 onDraw() 方法中包含 i/o 代码是好的/正常做法还是应该以某种方式分开?抱歉,我是 Android 新手:)
    • 我正在使用此代码保存在 DrawableView (github.com/PaNaVTEC/DrawableView) 上生成的位图,并且 PorterDuff.Mode.DARKEN 行导致 drawableView 中的位图不与原始位图一起保存图片。删除该行解决了问题,感谢您的代码,这很容易
    【解决方案2】:

    您可以尝试以下方法。 如果可以,将图像绘制到画布上:

    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】:

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

      阅读external storage 以获取有关如何写入 sd 卡的说明。

      【讨论】:

      • 嗨,我已经可以写入 sdcard 并且我的应用程序已经使用了您建议的位图方法。问题是我的显示器现在有 3 个位图来产生所需的效果。我知道如何保存一张图片只是不 3。我已经用我的 onDraw 更新了我的帖子
      猜你喜欢
      • 1970-01-01
      • 2014-07-28
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多