java rgb565,Java图像转换为RGB565

I try to convert image to RGB565 format.

I read this image:

BufferedImage bufImg = ImageIO.read(imagePathFile);

sendImg = new BufferedImage(CONTROLLER_LCD_WIDTH/*320*/, CONTROLLER_LCD_HEIGHT/*240*/, BufferedImage.TYPE_USHORT_565_RGB);

sendImg.getGraphics().drawImage(bufImg, 0, 0, CONTROLLER_LCD_WIDTH/*320*/, CONTROLLER_LCD_HEIGHT/*240*/, null);

Here is it:

VNB0W.png

Then I convert it to RGB565:

int numByte=0;

byte[] OutputImageArray = new byte[CONTROLLER_LCD_WIDTH*CONTROLLER_LCD_HEIGHT*2];

int i=0;

int j=0;

int len = OutputImageArray.length;

for (i=0;i

for (j=0;j

Color c = new Color(sendImg.getRGB(i, j));

int aRGBpix = sendImg.getRGB(i, j);

int alpha;

int red = c.getRed();

int green = c.getGreen();

int blue = c.getBlue();

//RGB888

red = (aRGBpix >> 16) & 0x0FF;

green = (aRGBpix >> 8) & 0x0FF;

blue = (aRGBpix >> 0) & 0x0FF;

alpha = (aRGBpix >> 24) & 0x0FF;

//RGB565

red = red >> 3;

green = green >> 2;

blue = blue >> 3;

//A pixel is represented by a 4-byte (32 bit) integer, like so:

//00000000 00000000 00000000 11111111

//^ Alpha ^Red ^Green ^Blue

//Converting to RGB565

short pixel_to_send = 0;

int pixel_to_send_int = 0;

pixel_to_send_int = (red << 11) | (green << 5) | (blue);

pixel_to_send = (short) pixel_to_send_int;

//dividing into bytes

byte byteH=(byte)((pixel_to_send >> 8) & 0x0FF);

byte byteL=(byte)(pixel_to_send & 0x0FF);

//Writing it to array - High-byte is second

OutputImageArray[numByte]=byteH;

OutputImageArray[numByte+1]=byteL;

numByte+=2;

}

}

Then I try to restore this from resulting array OutputImageArray:

i=0;

j=0;

numByte=0;

BufferedImage NewImg = new BufferedImage(CONTROLLER_LCD_WIDTH, CONTROLLER_LCD_HEIGHT, BufferedImage.TYPE_USHORT_565_RGB);

for (i=0;i

for (j=0;j

int curPixel=0;

int alpha=0x0FF;

int red;

int green;

int blue;

byte byteL=0;

byte byteH=0;

byteH = OutputImageArray[numByte];

byteL = OutputImageArray[numByte+1];

curPixel= (byteH << 8) | (byteL);

//RGB565

red = (curPixel >> (6+5)) & 0x01F;

green = (curPixel >> 5) & 0x03F;

blue = (curPixel) & 0x01F;

//RGB888

red = red << 3;

green = green << 2;

blue = blue << 3;

//aRGB

curPixel = 0;

curPixel = (alpha << 24) | (red << 16) | (green << 8) | (blue);

NewImg.setRGB(i, j, curPixel);

numByte+=2;

}

}

I output this restored image. But I see that it looks very poor.

ySSO0.png

I expected the lost of pictures quality.

But as I thought, this picture has to have almost the same quality as the previous picture. Is it right?

Is my code right?

解决方案

The reason you see these yellow artefacts is simply due to negative values for byteL overwriting bits from byteH containing the correct values for red and (part of) green channels. Let me explain.

Remember, if the highest bit in a byte is set to 1, the value is considered negative (-128 to -1 instead of 128 to 255), and by converting it to an int all the extra high-bits are set to 1 to conserve the same values (-128 to -1).

In your program, these extra bits set to 1 are in direct conflict with the value in byteH when applying the OR bit-operator |, overwriting (saturating) the red and (part of) green values you are trying to extract and display.

curPixel = (byteH << 8) | (byteL); // BUG: issue with negative byteL values

A solution is to apply an AND-mask to be sure to get rid of any unwanted bits before applying the OR bit-operator.

curPixel = byteL & 0xFF; // Convert byte to int to be within [0 , 255]

curPixel = (byteH << 8) | curPixel; // Apply OR bit-operator

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值