访问ARGB_8888 Android Bitmap中的原始数据

我试图使用
copyPixelsToBuffer
copyPixelsFromBuffer
方法在Android上以ARGB_8888格式访问位图的原始数据。但是,调用这些调用似乎始终将alpha通道应用于rgb通道。我需要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频道的顺序不同;没关系。但我没有 希望alpha通道应用于每个副本(这似乎是它正在做的)。 这是
copyPixelsToBuffer
copyPixelsFromBuffer
应该如何工作?有没有办法在byte []中获取原始数据? 添加以回答以下答案: 在
copyPixelsToBuffer
之前加入
buffer.order(ByteOrder.nativeOrder());
会改变结果,但仍然不是我想要的方式:
pixel before = ef234567
value before = ef614121
value after = ffffff41
pixel after = ff41ffff
似乎遭受了基本相同的问题(每个ѭ9应用alpha)。     
已邀请:
我意识到这是非常陈旧的,现在可能对你没有帮助,但我最近遇到了这个问题,试图让
copyPixelsFromBuffer
在我的应用程序中工作。 (感谢您提出这个问题,顺便说一句!您在调试中节省了大量时间。)我正在添加这个答案,希望它能帮助像我这样的其他人... 虽然我还没有使用它来确保它的工作原理,但从API级别19开始,我们终于有了一种方法来指定不要在
Bitmap
中“应用alpha”(a.k.a。预乘)。他们正在添加一个
setPremultiplied(boolean)
方法,通过允许我们指定
false
来帮助这种情况。 我希望这有帮助!     
我的猜测是,这可能与你正在使用的ByteBuffer的字节顺序有关。 ByteBuffer默认使用big endian。 用缓冲区设置缓冲区
buffer.order(ByteOrder.nativeOrder());
看看它是否有帮助。 此外,copyPixelsFromBuffer / copyPixelsToBuffer不会以任何方式更改像素数据。他们被复制原始。     
在Bitmap中访问数据的一种方法是使用getPixels()方法。下面你可以找到一个例子,我用来从argb数据中获取灰度图像,然后从字节数组返回到Bitmap(当然,如果你需要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;
   }

}
    
这是一个老问题,但我遇到了同样的问题,并且只是发现位图字节是预乘的,你可以设置位图(从API 19开始)不预先乘以缓冲区,但是在API中他们不保证。 来自文档:   
public final void setPremultiplied(boolean premultiplied)
     设置位图是否应将其数据视为预乘。   出于性能原因,位图始终被视图系统和Canvas预先乘以。如果在框架中绘制,则在位图中存储未预先乘的数据(通过
setPixel
setPixels
BitmapFactory.Options.inPremultiplied
)会导致不正确的混合。      此方法不会影响没有Alpha通道的位图的行为,或者如果
hasAlpha()
返回false。      使用颜色未预乘的源位图调用
createBitmap
createScaledBitmap
可能会产生
RuntimeException
,因为这些函数需要绘制源,而非预乘的位图不支持。     

要回复问题请先登录注册