且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java 图片提取RGB数组 RGBOfCharMaps (整理)

更新时间:2022-08-12 20:59:13

  1 package demo;
  2 
  3 /**
  4  *                Java 图片提取RGB数组 RGBOfCharMaps (整理)
  5  * 声明:
  6  *     和ImageCombining配合使用的工具,这里是提取图片的R、G、B生成数组,放入文件
  7  * 中,给ImageCombining进行图片合成。
  8  *
  9  *                                             2016-1-2 深圳 南山平山村 曾剑锋
 10  */
 11 
 12 import java.awt.image.BufferedImage;
 13 import java.io.BufferedOutputStream;
 14 import java.io.File;
 15 import java.io.FileOutputStream;
 16 import java.io.IOException;
 17 import java.util.regex.Matcher;
 18 import java.util.regex.Pattern;
 19 import javax.imageio.ImageIO;
 20 import javax.swing.JFileChooser;
 21 import javax.swing.filechooser.FileNameExtensionFilter;
 22 
 23 public class RGBOfCharMaps{
 24     /** 声明一个文件选择器引用 */
 25     static JFileChooser jFileChooser = null;
 26     /** 用于保存您选择的单个或者多个文件路径集合, 初始化为null */
 27     static File filePath = null;
 28     /** 保存图片的宽、高 */
 29     static int imageWidth = 0;
 30     static int imageHeight = 0;
 31     /** 图像缓冲引用 */
 32     static BufferedImage bufferedImage = null;
 33     /**
 34      * main()函数,完成任务如下:<br><ol>
 35      *         <li>对文件选择器进行初始化;<br>
 36      *         <li>保存转换好的文件;<br>
 37      *         <li>如果出现异常,给出提示信息。<br></ol>
 38      */
 39     public static void main(String[] args) {
 40         try {
 41             filesSelectInit();
 42             System.out.println(1);
 43             if (getImageFile()) {
 44                 fileSave();  
 45                 System.out.println(imageHeight);
 46                 System.out.println(imageWidth);
 47             };
 48               
 49         } catch (Exception e) {
 50             //System.out.println("请选择后缀为png/PNG/jpeg/jpe/JPEG的文件");
 51             System.out.println(e);
 52         }
 53     }
 54 
 55 
 56     private static boolean getImageFile() throws IOException {
 57         if (jFileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
 58             filePath = jFileChooser.getSelectedFile();
 59             if (fileSuffixCheck(filePath)) {
 60                 bufferedImage = ImageIO.read(filePath);
 61                 imageWidth = bufferedImage.getWidth();
 62                 imageHeight = bufferedImage.getHeight();
 63                 return true;
 64             }
 65         }
 66         return false;
 67     }
 68 
 69 
 70     /**
 71      * 文件后缀检查函数,完成任务如下:<br>
 72      * &nbsp &nbsp &nbsp &nbsp
 73      *         采用正则表达式对文件进行匹配。<br>
 74      */
 75     private static boolean fileSuffixCheck(File filePath) {
 76         //使用正则表达式来防止选择目前不支持的文件,目前只支持png/PNG/jpeg/jpe/JPEG格式图片
 77         Pattern pattern = Pattern.compile(".+[.][pPJj][nNpP][eEgGpP][gG]?");
 78         Matcher matcher = pattern.matcher(filePath.getName());
 79         if (matcher.matches() == false) {
 80             return false;
 81         }
 82         return true;
 83     }
 84     /**
 85      * 文件保存函数,完成任务如下:<br><ol>
 86      *         <li>设置一个文件保存的路径,这个路径可以自己修改;<br>
 87      *         <li>创建文件路径下的文件缓冲区;<br>
 88      *         <li>将charMaps中的字符写好文件中,忽略在上、下、左、右边界之外的部分,只将边界内的字符输出;<br>
 89      *         <li>每写完一行字符,进行换行;<br>
 90      *         <li>最后关闭文件缓冲区,如果不关闭,文件缓冲区内的字符可能不会写到文件中,请注意;<br>
 91      *         <li>提示完成以及提示文件路径。<br><ol>
 92      */
 93     private static void fileSave() {
 94         File[] saveFilePath = new File[3];
 95         saveFilePath[0] = new File("/home/soft1/B.txt");
 96         saveFilePath[1] = new File("/home/soft1/G.txt");
 97         saveFilePath[2] = new File("/home/soft1/R.txt");
 98         try {
 99             SaveRGB(saveFilePath);    
100         } catch (IOException e1) {
101             e1.printStackTrace();
102         }
103     }
104 
105     private static void SaveRGB(File[] saveFilePath) throws IOException {
106         String[] RGB = {"Blue","Green","Red"};
107         StringBuilder stringBuilder = new StringBuilder(); 
108         for (int i = 0; i < saveFilePath.length; i++) {
109             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(saveFilePath[i]));
110             singleColorSave(stringBuilder,bufferedOutputStream,i);
111             System.out.println("CharMaps已完成颜色"+RGB[i]+"工作,请到"+saveFilePath[i].getPath()+"中查看结果 ^_^\n");
112         }        
113     }
114     private static void singleColorSave(StringBuilder stringBuilder,
115             BufferedOutputStream bufferedOutputStream, int i) throws IOException {
116         stringBuilder.append('{');
117         for (int row = 0; row < imageHeight; row++) {
118             stringBuilder.append('{');
119             for (int col = 0; col < imageWidth; col++) {
120                 int rgb = bufferedImage.getRGB(col, row);
121                 int singleColor = ((rgb >> (8*i))&0xff);
122                 stringBuilder.append(singleColor);
123                 stringBuilder.append(',');
124             }
125             stringBuilder.append('}');
126             if (row == imageHeight-1) {
127                 stringBuilder.append('}');
128             }else {
129                 stringBuilder.append(',');
130             }
131             byte[] byteWrite = (byte[])stringBuilder.toString().getBytes();
132             bufferedOutputStream.write(byteWrite, 0, stringBuilder.length());
133             bufferedOutputStream.write('\n');
134             bufferedOutputStream.flush();
135             stringBuilder.delete(0, stringBuilder.length());
136         }
137         bufferedOutputStream.close();
138     }
139 
140 
141     /**
142      * 文件选择对话框初始化函数,Init是初始化的英文单词缩写,完成任务如下:<br><ol>
143      *         <li>提示欢迎使用CharMaps;<br>
144      *         <li>创建文件选择对话框;<br>
145      *         <li>创建文件选择过滤器;<br>
146      *         <li>将文件选择过滤器添加进入文件对话框,还句话说是:使文件选择过滤器有效;<br>
147      *         <li>将文件选择对话框设置为可以多选;<br>
148      *         <li>提示完成初始化。<br></ol>
149      */
150     private static void filesSelectInit() {
151         System.out.println("\n\t欢迎使用RGBOfCharMaps");
152         jFileChooser = new JFileChooser();
153         FileNameExtensionFilter filter = new FileNameExtensionFilter(
154                 "Images", "jpg", "png","PNG","JPG","jpe","JPE");
155         jFileChooser.setFileFilter(filter);
156         System.out.println("1、完成文件选择初始化");
157     }
158 }

 

RGBOfCharMaps