java type int argb,TYPE_INT_RGB和TYPE_INT_ARGB的格式

Could anyone explain for me how java stores color in TYPE_INT_RGB and TYPE_INT_ARGB ?

Do these lines of code work properly for calculating red, green and blue ?

int red= (RGB>>16)&255;

int green= (RGB>>8)&255;

int blue= (RGB)&255;

And what about TYPE_INT_ARGB ? How can I get red, green and blue from TYPE_INT_ARGB?

解决方案

The TYPE_INT_ARGB represents Color as an int (4 bytes) with alpha channel in bits 24-31, red channels in 16-23, green in 8-15 and blue in 0-7.

The TYPE_INT_RGB represents Color as an int (4 bytes) int the same way of TYPE_INT_ARGB, but the alpha channel is ignored (or the bits 24-31 are 0).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是argb888与rgb888转换程序的示例代码: ```java public static int[] argb888ToRgb888(int[] argb888) { int[] rgb888 = new int[argb888.length]; for (int i = 0; i < argb888.length; i++) { int argb = argb888[i]; int alpha = (argb >> 24) & 0xff; int red = (argb >> 16) & 0xff; int green = (argb >> 8) & 0xff; int blue = (argb) & 0xff; int rgb = ((red << 16) | (green << 8) | (blue)); rgb888[i] = rgb; } return rgb888; } ``` 而将Android camera2 api YUV_420_888转换为RGB的程序,则需要使用ColorSpace和ColorConverter类来进行转换。示例代码如下: ```java private void convertYuvToRgb(Image image) { Image.Plane[] planes = image.getPlanes(); ByteBuffer yBuffer = planes[0].getBuffer(); ByteBuffer uBuffer = planes[1].getBuffer(); ByteBuffer vBuffer = planes[2].getBuffer(); int ySize = yBuffer.remaining(); int uSize = uBuffer.remaining(); int vSize = vBuffer.remaining(); byte[] yBytes = new byte[ySize]; byte[] uBytes = new byte[uSize]; byte[] vBytes = new byte[vSize]; yBuffer.get(yBytes); uBuffer.get(uBytes); vBuffer.get(vBytes); ColorSpace cs = ColorSpace.get(ColorSpace.Named.SRGB); ColorSpace csYuv = ColorSpace.get(ColorSpace.Named.YUV_420_888); ColorConverter cc = new ColorConverter(csYuv, cs); float[] yuvFloat = new float[yBytes.length + uBytes.length + vBytes.length]; for (int i = 0; i < ySize; i++) { yuvFloat[i] = (float) (yBytes[i] & 0xff); } for (int i = 0; i < uSize; i++) { yuvFloat[ySize + i] = (float) (uBytes[i] & 0xff); } for (int i = 0; i < vSize; i++) { yuvFloat[ySize + uSize + i] = (float) (vBytes[i] & 0xff); } float[] rgbFloat = new float[yBytes.length * 3]; cc.convert(yuvFloat, rgbFloat); int[] rgb888 = new int[yBytes.length]; for (int i = 0; i < yBytes.length; i++) { int r = (int) rgbFloat[i * 3]; int g = (int) rgbFloat[i * 3 + 1]; int b = (int) rgbFloat[i * 3 + 2]; rgb888[i] = (r << 16) | (g << 8) | b; } } ``` 注意:上述代码仅为示例代码,实际应用中可能需要根据具体情况进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值