java rgb565,Java图像转换为RGB565

java rgb565,Java图像转换为RGB565Itrytoconver Ireadthisima BufferedImag ImageIO read imagePathFil sendImg newBufferedI CONTROLLER LCD WIDTH 320 CONTROLLER LCD HEIGHT 240 Buff

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

//^ 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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/222346.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午3:59
下一篇 2026年3月17日 下午4:00


相关推荐

  • 认识EJB_ej是什么的缩写

    认识EJB_ej是什么的缩写一、定义       将业务逻辑从客户端软件中抽取出来,封装在一个组件中。这个组件运行在一个独立的服务器上,客户端软件通过网络调用组件提供的服务以实现业务逻辑,而客户端软件的功能单纯到只负责发送调用请求和显示处理结果。在J2EE中,这个运行在一个独立的服务器上,并封装了业务逻辑的组件就是EJB(EnterpriseJavaBean)组件。EJB体系结构中涉及以下6类软件构件:1

    2025年7月2日
    8
  • 如何使用一套键盘鼠标,同时控制多台电脑_控制鼠标

    如何使用一套键盘鼠标,同时控制多台电脑_控制鼠标1.蓝牙键盘我使用的蓝牙键盘是GANSSGS87键的蓝牙双模键盘茶轴,既支持有线,也支持无线。最大的优点是便宜,到手300多,这个价格能买到有牌子、质量还不错的机械键盘算是非常难得的。当然也有一点小瑕疵,就是不能充电,得用电池,不过大半年才换一次电池,这个缺陷也可以忽略了。接下来记录一下该键盘的蓝牙连接的设置步骤,其他键盘应该也是同理,希望能给大家一些参与:先选择你要设置的键:比如你想把Fn+Q,作为切换到Mac的快捷键,那么你先按Fn+Q,表示已经进入这个快捷键的作用域下。

    2022年10月15日
    3
  • 去掉豆包文本星号

    去掉豆包文本星号

    2026年3月12日
    3
  • Jmeter性能测试云平台搭建

    Jmeter性能测试云平台搭建本篇文章主要讲解 Jmeter 性能测试云平台搭建 这是我们在进行 DevOps 云平台中性能测试一部分 后期结合 docker 容器技术进行集群的动态扩展

    2026年3月18日
    2
  • idea常用快捷键

    idea常用快捷键idea常用快捷键

    2022年4月22日
    42
  • Java标识符命名规则(超详细!)[通俗易懂]

    Java标识符命名规则(超详细!)[通俗易懂]规则1:标识符只能由数字、字母(包括中文)、下划线_、美元符号$组成,不能含有其它符号。规则2:标识符不能以数字开头规则3:关键字不能做标识符。例如:publicclassstaticvoid这些蓝色的字体都是关键字,关键字是不能做标识符的。规则4:标识符是严格区分大小写的。大写A和小写a不一样。规则5:标识符理论上是没有长度限制的。…

    2022年7月7日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号