【问题标题】:amdroid overlay 1 bitmap to another image not getting proper outputandroid覆盖1位图到另一个图像没有得到正确的输出
【发布时间】:2014-09-01 18:51:24
【问题描述】:

在我的一个应用程序中,我需要进行遮罩(将 1 张图像重叠到另一张图像)

在我的应用程序中,我必须将 1 个图像(位图)加载到 imageview,然后必须将一些框架应用于该图像,我为此使用了另一个 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;
    }

我收到

但我需要它

【问题讨论】:

标签: android bitmap overlap masking


【解决方案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;
    }

【讨论】:

  • 这是完美的解决方案。
【解决方案2】:
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;
}

【讨论】:

  • 我不明白这个问题?要执行叠加,您需要一个画布,您必须在其上绘制主图像和用于叠加的图像。为了创建一个图像在另一个图像之上的效果,Paint 对象用于设置不透明度(使顶部图像更透明)。
  • 你能给我一些链接吗?
  • 请查看代码并尝试在您的应用程序中实现它,因为代码是您上面要求的公平表示。我不确定您为什么需要链接。
  • 在你的代码中你没有指定什么是覆盖?
  • 在这一行中 int centerX = (canvasWidth - overlay.getWidth()) /2; int centerY = (canvasHeight - overlay.getHeight()) /2 ;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 2014-03-12
  • 1970-01-01
相关资源
最近更新 更多