STM32显示图片,将图片转换为十六进制数组便捷工具

自己研究STM32 TFT显示图片的时候,没有找到合适的将图片数组化的工具,参照几位大佬的博客,自己写了一个java工具。代码如下。将图片生成数组打印在控制台,自己用记得修改图片路径。

  
 
import java.awt.AWTException;  
import java.awt.Dimension;  
import java.awt.Rectangle;  
import java.awt.Robot;  
import java.awt.Toolkit;  
import java.awt.image.BufferedImage;  
import java.io.File;  
 
import javax.imageio.ImageIO;  
 
public class ReadColorTest {  
   /** 
    * 读取一张图片的RGB值 
    *  
    * @throws Exception 
    */  
   public void getImagePixel(String image) throws Exception {  
       int[] rgb = new int[3];  
       File file = new File(image);  
       BufferedImage bi = null;  
       try {  
           bi = ImageIO.read(file);  
       } catch (Exception e) {  
           e.printStackTrace();  
       }  
       int width = bi.getWidth();  
       int height = bi.getHeight();  
       int minx = bi.getMinX();  
       int miny = bi.getMinY();  
       System.out.println("width=" + width + ",height=" + height + ".");  
       System.out.println("minx=" + minx + ",miniy=" + miny + ".");  
       String str;
       System.out.println("[");
       int cou = 0;
       for (int i = minx; i < width; i++) {  
           for (int j = miny; j < height; j++) {  
               int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字  
               int res = ConvertTo16(pixel);
             /*  rgb[0] = (pixel & 0xff0000) >> 16;  
               rgb[1] = (pixel & 0xff00) >> 8;  
               rgb[2] = (pixel & 0xff);  
               System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + ","  
                       + rgb[1] + "," + rgb[2] + ")");  */
              
              str = Integer.toHexString(res);  
             if(cou>=15){
             
             System.out.println("");
             cou=0;
             }
               System.out.print("0x"+str+",");
               cou++;
           }  
       }  
   }  
   
   int ConvertTo16(int color)


   {


     int r,g,b;


     b = (color>>3) & 0x1f; 


    g = (color>>(8+2)) & 0x3f;


    r = (color>>(16+3)) & 0x1f;


    return ((r<<11) | (g<<5) | (b<<0));
  
   }
 
   /** 
    * 返回屏幕色彩值 
    *  
    * @param x 
    * @param y 
    * @return 
    * @throws AWTException 
    */  
   public int getScreenPixel(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。  
       Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。  
       rb = new Robot();  
       Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包  
       Dimension di = tk.getScreenSize(); // 屏幕尺寸规格  
       System.out.println(di.width);  
       System.out.println(di.height);  
       Rectangle rec = new Rectangle(0, 0, di.width, di.height);  
       BufferedImage bi = rb.createScreenCapture(rec);  
       int pixelColor = bi.getRGB(x, y);  
 
       return 16777216 + pixelColor; // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。  
   }  
 
   /** 
    * @param args 
    */  
   public static void main(String[] args) throws Exception {  
       int x = 0;  
       ReadColorTest rc = new ReadColorTest();  
       x = rc.getScreenPixel(100, 345);  
       System.out.println(x + " - ");  
       rc.getImagePixel("C:\\1.jpg");  
   }  
 
}  

参考的大佬博客:

https://blog.csdn.net/ubuntu_yanglei/article/details/46443929;

https://blog.csdn.net/typa01_kk/article/details/46673187;


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值