android - 获取ARGB_8888 Android Bitmap中的原始数据

标签 android bitmap java-native-interface alpha-transparency argb

我正在尝试使用 copyPixelsToBuffercopyPixelsFromBuffer 方法在 Android 上访问 ARGB_8888 格式的位图原始数据。但是,调用这些调用似乎总是将 alpha channel 应用于 rgb channel 。我需要 byte[] 或类似格式的原始数据(通过 JNI;是的,我知道 Android 2.2 中的 bitmap.h,但不能使用它)。

这是一个示例:

    // Create 1x1 Bitmap with alpha channel, 8 bits per channel
    Bitmap one = Bitmap.createBitmap(1,1,Bitmap.Config.ARGB_8888);
    one.setPixel(0,0,0xef234567);
    Log.v("?","hasAlpha() = "+Boolean.toString(one.hasAlpha()));
    Log.v("?","pixel before = "+Integer.toHexString(one.getPixel(0,0)));

    // Copy Bitmap to buffer
    byte[] store = new byte[4];
    ByteBuffer buffer  = ByteBuffer.wrap(store);
    one.copyPixelsToBuffer(buffer);

    // Change value of the pixel
    int value=buffer.getInt(0);
    Log.v("?", "value before = "+Integer.toHexString(value));
    value = (value >> 8) | 0xffffff00;
    buffer.putInt(0, value);
    value=buffer.getInt(0);
    Log.v("?", "value after = "+Integer.toHexString(value));

    // Copy buffer back to Bitmap
    buffer.position(0);
    one.copyPixelsFromBuffer(buffer);
    Log.v("?","pixel after = "+Integer.toHexString(one.getPixel(0,0)));

然后日志显示

hasAlpha() = true
pixel before = ef234567
value before = 214161ef
value after = ffffff61
pixel after = 619e9e9e

我了解 argb channel 的顺序不同;没关系。但我不 希望将 alpha channel 应用于每个副本(这似乎是在做的)。

copyPixelsToBuffercopyPixelsFromBuffer 应该是这样工作的吗?是否有任何方法来获取 byte[] 中的原始数据?

为响应以下回答而添加:

copyPixelsToBuffer 之前放入 buffer.order(ByteOrder.nativeOrder()); 确实改变了结果,但仍然不是我想要的方式:

pixel before = ef234567
value before = ef614121
value after = ffffff41
pixel after = ff41ffff

似乎遇到了本质上相同的问题(alpha 应用于每个 copyPixelsFrom/ToBuffer)。

最佳答案

访问位图中数据的一种方法是使用 getPixels() 方法。下面你可以找到我用来从 argb 数据获取灰度图像,然后从字节数组返回到位图的示例(当然,如果你需要 rgb,你保留 3x 字节并将它们全部保存......):

/*Free to use licence by Sami Varjo (but nice if you retain this line)*/

public final class BitmapConverter {

    private BitmapConverter(){};

   /**
    * Get grayscale data from argb image to byte array
    */
   public static byte[] ARGB2Gray(Bitmap img)
   {

       int width = img.getWidth();
       int height = img.getHeight();

       int[] pixels = new int[height*width];
       byte grayIm[] = new byte[height*width];

       img.getPixels(pixels,0,width,0,0,width,height);

       int pixel=0;
       int count=width*height;

       while(count-->0){
           int inVal = pixels[pixel];

           //Get the pixel channel values from int 
           double r = (double)( (inVal & 0x00ff0000)>>16 );
           double g = (double)( (inVal & 0x0000ff00)>>8  );
           double b = (double)(  inVal & 0x000000ff)      ;

           grayIm[pixel++] = (byte)( 0.2989*r + 0.5870*g + 0.1140*b );
       }

       return grayIm;
   }

   /**
    * Create a gray scale bitmap from byte array
    */
   public static Bitmap gray2ARGB(byte[] data, int width, int height)
   {
       int count = height*width;
       int[] outPix = new int[count];
       int pixel=0;
       while(count-->0){
           int val = data[pixel] & 0xff; //convert byte to unsigned
           outPix[pixel++] = 0xff000000 | val << 16 | val << 8 | val ;
       }

       Bitmap out =  Bitmap.createBitmap(outPix,0,width,width, height, Bitmap.Config.ARGB_8888);
       return out;
   }

}

关于android - 获取ARGB_8888 Android Bitmap中的原始数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5010545/

相关文章:

android - 如何在 Android 和 Linux 上将 JPG 文件解码为相同的位图?

python autopy问题/困惑

android - 如何使用 JNI 将数据传递到构造函数

java - 我必须在 Eclipse 中使用 JNI 从我的 C 程序访问 java 程序中声明的变量

java - 从 JNI 中的 native 代码退出会杀死 Java 应用程序

java - 最适合性能的编码风格

java - 如何在静态上使用 getString() ?

android - 如何通过编程获取联系人的最大联系人ID?

android - TYPE_KEYGUARD 调用电话后保持全屏模式

android - 空指针异常 : Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()'