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

1 packagedemo;2

3 /**

4 * Java 图片提取RGB数组 RGBOfCharMaps (整理)5 * 声明:6 * 和ImageCombining配合使用的工具,这里是提取图片的R、G、B生成数组,放入文件7 * 中,给ImageCombining进行图片合成。8 *9 * 2016-1-2 深圳 南山平山村 曾剑锋10 */

11

12 importjava.awt.image.BufferedImage;13 importjava.io.BufferedOutputStream;14 importjava.io.File;15 importjava.io.FileOutputStream;16 importjava.io.IOException;17 importjava.util.regex.Matcher;18 importjava.util.regex.Pattern;19 importjavax.imageio.ImageIO;20 importjavax.swing.JFileChooser;21 importjavax.swing.filechooser.FileNameExtensionFilter;22

23 public classRGBOfCharMaps{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()函数,完成任务如下:

  1. 35 *
  2. 对文件选择器进行初始化;
    36 *
  3. 保存转换好的文件;
    37 *
  4. 如果出现异常,给出提示信息。
38 */

39 public static voidmain(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() throwsIOException {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 * 文件后缀检查函数,完成任务如下:
72 * &nbsp &nbsp &nbsp &nbsp73 * 采用正则表达式对文件进行匹配。
74 */

75 private static booleanfileSuffixCheck(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 * 文件保存函数,完成任务如下:

  1. 86 *
  2. 设置一个文件保存的路径,这个路径可以自己修改;
    87 *
  3. 创建文件路径下的文件缓冲区;
    88 *
  4. 将charMaps中的字符写好文件中,忽略在上、下、左、右边界之外的部分,只将边界内的字符输出;
    89 *
  5. 每写完一行字符,进行换行;
    90 *
  6. 最后关闭文件缓冲区,如果不关闭,文件缓冲区内的字符可能不会写到文件中,请注意;
    91 *
  7. 提示完成以及提示文件路径。
    1. 92 */

    93 private static voidfileSave() {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) throwsIOException {106 String[] RGB = {"Blue","Green","Red"};107 StringBuilder stringBuilder = newStringBuilder();108 for (int i = 0; i < saveFilePath.length; i++) {109 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(newFileOutputStream(saveFilePath[i]));110 singleColorSave(stringBuilder,bufferedOutputStream,i);111 System.out.println("CharMaps已完成颜色"+RGB[i]+"工作,请到"+saveFilePath[i].getPath()+"中查看结果 ^_^\n");112 }113 }114 private static voidsingleColorSave(StringBuilder stringBuilder,115 BufferedOutputStream bufferedOutputStream, int i) throwsIOException {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是初始化的英文单词缩写,完成任务如下:

    1. 143 *
    2. 提示欢迎使用CharMaps;
      144 *
    3. 创建文件选择对话框;
      145 *
    4. 创建文件选择过滤器;
      146 *
    5. 将文件选择过滤器添加进入文件对话框,还句话说是:使文件选择过滤器有效;
      147 *
    6. 将文件选择对话框设置为可以多选;
      148 *
    7. 提示完成初始化。
    149 */

    150 private static voidfilesSelectInit() {151 System.out.println("\n\t欢迎使用RGBOfCharMaps");152 jFileChooser = newJFileChooser();153 FileNameExtensionFilter filter = newFileNameExtensionFilter(154 "Images", "jpg", "png","PNG","JPG","jpe","JPE");155 jFileChooser.setFileFilter(filter);156 System.out.println("1、完成文件选择初始化");157 }158 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值