【问题标题】:subtracting a Bitamap image from another java android从另一个 java android 中减去 Bitamap 图像
【发布时间】:2014-02-05 08:16:39
【问题描述】:

如何在 Android 中从另一个位图图像中减去一个位图图像。请帮助提供相同的代码

【问题讨论】:

标签: java android bitmap subtraction


【解决方案1】:

虽然为时已晚,但我将答案发布给其他人,他们可能最终会在这里寻找一种减去图像的方法,就像我在这里结束时一样。

您可以使用 Bitmps 类的 getPixel() 来获取 Image 中每个点的颜色值。一旦你得到 2 个位图的这个值,你可以减去 ARGB 值来得到结果图像。

下面是代码sn-p

 Bitmap image1 = BitmapFactory.decodeFile(imgFile1.getAbsolutePath());
 Bitmap image2 = BitmapFactory.decodeFile(imgFile2.getAbsolutePath());
 Bitmap image3 = Bitmap.createBitmap(image1.getWidth(), image1.getHeight(), Config.ARGB_8888);

 for(int x = 0; x < image1.getWidth(); x++)
     for(int y = 0; y < image1.getHeight(); y++) {
         int argb1 = image1.getPixel(x, y);
         int argb2 = image2.getPixel(x, y);

         //int a1 = (argb1 >> 24) & 0xFF;
         int r1 = (argb1 >> 16) & 0xFF;
         int g1 = (argb1 >>  8) & 0xFF;
         int b1 = argb1 & 0xFF;

         //int a2 = (argb2 >> 24) & 0xFF;
         int r2 = (argb2 >> 16) & 0xFF;
         int g2 = (argb2 >>  8) & 0xFF;
         int b2 = argb2 & 0xFF;

         //int aDiff = Math.abs(a2 - a1);
         int rDiff = Math.abs(r2 - r1);
         int gDiff = Math.abs(g2 - g1);
         int bDiff = Math.abs(b2 - b1);

         int diff = 
              (255 << 24) | (rDiff << 16) | (gDiff << 8) | bDiff;

         image3.setPixel(x, y, diff);
     }


 try (FileOutputStream out = new FileOutputStream(filename)) {
     image3.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
     // PNG is a lossless format, the compression factor (100) is ignored
 } catch (IOException e) {
     e.printStackTrace();
 }

学分

https://stackoverflow.com/a/21806219/9640177

https://stackoverflow.com/a/16804467/9640177

https://stackoverflow.com/a/673014/9640177

https://stackoverflow.com/a/5392792/9640177

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-30
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多