问题:

想写一个小程序可读取图片的色深(bit-depth)。网上有一些软件可完成这个功能,但是我想把程序做成一个可移植的插件。

本想用c写的,但实在麻烦,最后选择java,与很多方法不用自己写,速度快。

最后打包成一个jar包,只要装了jdk就可以在控制台运行。

我用的是MYECLIPSE,步骤如下:
1.创建一个工程;

2.创建一个java class;

3.程序包含两个类getinfo.java 和 methodclass.java;

getinfo.java包含main()方法,代码如下:

  1. import java.io.File;
  2.  
  3. public class getinfo {
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. String url;
  7. if (args.length != 0) {
  8. url = args[0];
  9. } else
  10. url = "F:\\tmpxu\\333.png";
  11. File f1 = new File(url);// "F:\\tmpxu\\333.png"
  12. methodclass my = new methodclass();
  13. my.ImageInfo(f1);
  14. System.out.println("getName====" + my.getName());
  15. System.out.println("getPath====" + my.getPath());
  16. System.out.println("getDate_created====" + my.getDate_created());
  17. System.out.println("getDate_modified====" + my.getDate_modified());
  18. System.out.println("getType====" + my.getType());
  19. System.out.println("getSize====" + my.getSize());
  20. System.out.println("getWidth====" + my.getWidth());
  21. System.out.println("getHeight====" + my.getHeight());
  22. System.out.println("getBit_depth====" + my.getBit_depth());
  23. }
  24. }

这里注意:

控制台命令: java -jar getImageInfo.jar 参数1 参数2 参数3……

其中getImageInfo.jar为最终生成的jar包,参数1 参数2 参数3……可作为main(String args[])的参数,从String args[]中得到。

methodclass.java类主要完成具体的读取图片信息的方法,支持png 、jpeg 、gif 、bmp格式。

代码大多来自这个链接:http://yuncode.net/code/c_53881eaa2532066,整理后的代码如下:

  1. import java.awt.image.BufferedImage;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.text.DecimalFormat;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import java.util.StringTokenizer;
  11. import javax.imageio.ImageIO;
  12.  
  13. public class methodclass {
  14. private String name = "";
  15. private String path = "";
  16. private String date_created = "";
  17. private String date_modified = "";
  18. private String bytes = "";
  19. private String type = "";
  20. private String size = "";
  21. private String width = "";
  22. private String height = "";
  23. private String bit_depth = "";
  24.  
  25. public void ImageInfo(File file) {
  26. name = file.getName();
  27. path = file.getParent();
  28. date_created = getDate_created(file);
  29. date_modified = new SimpleDateFormat("yyyy/MM/dd HH:mm")
  30. .format(new Date(file.lastModified()));
  31. bytes = getBytes(file);
  32. getImageData(file);
  33. getImageFileData(file);
  34. }
  35.  
  36. // 判读图片类型
  37. private void getImageFileData(File file) {
  38. try {
  39. FileInputStream input = new FileInputStream(file);
  40. /*
  41. * he java.io.FileInputStream.available() method returns number of
  42. * remaining bytes that can be read from this input stream without
  43. * blocking by the next method call for this input stream. The next
  44. * method call can also be the another thread.
  45. * 通过available方法取得流的最大字符数
  46. */
  47. byte[] b = new byte[input.available()];
  48. if (b.length == 0) {
  49. System.out.print("the file is empty!!!");
  50. return;
  51. }
  52. input.read(b);
  53. input.close();
  54. int b1 = b[0] & 0xff;
  55. int b2 = b[1] & 0xff;
  56. if (b1 == 0x42 && b2 == 0x4d) {
  57. checkBmp(b);
  58. } else if (b1 == 0x47 && b2 == 0x49) {
  59. checkGif(b);
  60. } else if (b1 == 0x89 && b2 == 0x50) {
  61. checkPng(b);
  62. } else if (b1 == 0xff && b2 == 0xd8) {
  63. checkJpeg(b, file);
  64. }
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69.  
  70. // 获得图片宽高
  71. private void getImageData(File file) {
  72. try {
  73. BufferedImage img = ImageIO.read(file);
  74. size = img.getWidth() + " x " + img.getHeight();
  75. width = img.getWidth() + " 像素";
  76. height = img.getHeight() + " 像素";
  77. } catch (IOException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81.  
  82. public void checkBmp(byte[] b) {
  83. type = "BMP 文件";
  84. int bitsPerPixel = (b[28] & 0xff) | (b[29] & 0xff) << 8;
  85. if (bitsPerPixel == 1 || bitsPerPixel == 4 || bitsPerPixel == 8
  86. || bitsPerPixel == 16 || bitsPerPixel == 24
  87. || bitsPerPixel == 32) {
  88. bit_depth = bitsPerPixel + "";
  89. }
  90. }
  91.  
  92. public void checkGif(byte[] b) {
  93. type = "GIF 文件";
  94. bit_depth = (b[10] & 0x07) + 1 + "";
  95. }
  96.  
  97. public void checkPng(byte[] b) {
  98. type = "PNG 图像";
  99. int bitsPerPixel = b[24] & 0xff;
  100. if ((b[25] & 0xff) == 2) {
  101. bitsPerPixel *= 3;
  102. } else if ((b[25] & 0xff) == 6) {
  103. bitsPerPixel *= 4;
  104. }
  105. bit_depth = bitsPerPixel + "";
  106. }
  107.  
  108. /*
  109. * (b[i] & 0xff):byte转换int时的运算 其原因在于:1.byte的大小为8bits而int的大小为32bits;
  110. * 2.java的二进制采用的是补码形式;
  111. * 如果不进行&0xff,那么当一个byte会转换成int时,由于int是32位,而byte只有8位这时会进行补位,
  112. * 例如补码11111111的十进制数为-1转换为int时变为32个1!和0xff相与后,高24比特就会被清0了,结果就对了。 bit_depth:
  113. * a 1 bit image, can only show two colors, black and white. That is because
  114. * the 1 bit can only store one of two values, 0 (white) and 1 (black). An 8
  115. * bit image can store 256 possible colors, while a 24 bit image can display
  116. * about 16 million colors.
  117. */
  118. public void checkJpeg(byte[] b, File file) {
  119. type = "JPEG 图像";
  120. int i = 2;
  121. while (true) {
  122. int marker = (b[i] & 0xff) << 8 | (b[i + 1] & 0xff);
  123. int size = (b[i + 2] & 0xff) << 8 | (b[i + 3] & 0xff);
  124. if (marker >= 0xffc0 && marker <= 0xffcf && marker != 0xffc4
  125. && marker != 0xffc8) {
  126. bit_depth = (b[i + 4] & 0xff) * (b[i + 9] & 0xff) + "";
  127. break;
  128. } else {
  129. i += size + 2;
  130. }
  131. }
  132. }
  133.  
  134. // 文件创建日期
  135. private String getDate_created(File file) {
  136. try {
  137. Process ls_proc = Runtime.getRuntime().exec(
  138. "cmd.exe /c dir \"" + file.getAbsolutePath() + "\" /tc");
  139. BufferedReader reader = new BufferedReader(new InputStreamReader(
  140. ls_proc.getInputStream()));
  141. for (int i = 0; i < 5; i++) {
  142. reader.readLine();
  143. }
  144. StringTokenizer st = new StringTokenizer(reader.readLine());
  145. String date = st.nextToken();
  146. String time = st.nextToken();
  147. reader.close();
  148. return date + " " + time;
  149. } catch (IOException e) {
  150. e.printStackTrace();
  151. }
  152. return "";
  153. }
  154.  
  155. // 读取文件大小
  156. private String getBytes(File file) {
  157. DecimalFormat df = new DecimalFormat();
  158. float length = (float) file.length();
  159. int p = 0;
  160. while (length > 1023) {
  161. length /= 1024;
  162. p++;
  163. }
  164. if (length > 99) {
  165. df.setMaximumFractionDigits(0);
  166. } else if (length > 9) {
  167. df.setMaximumFractionDigits(1);
  168. } else {
  169. df.setMaximumFractionDigits(2);
  170. }
  171. if (p == 0) {
  172. return (int) length + " 字节";
  173. } else if (p == 1) {
  174. return df.format(length) + " KB";
  175. } else if (p == 2) {
  176. return df.format(length) + " MB";
  177. } else if (p == 3) {
  178. return df.format(length) + " GB";
  179. } else {
  180. return df.format(length) + " TB";
  181. }
  182. }
  183.  
  184. public String getName() {
  185. return name;
  186. }
  187.  
  188. public String getPath() {
  189. return path;
  190. }
  191.  
  192. public String getDate_created() {
  193. return date_created;
  194. }
  195.  
  196. public String getDate_modified() {
  197. return date_modified;
  198. }
  199.  
  200. public String getBytes() {
  201. return bytes;
  202. }
  203.  
  204. public String getType() {
  205. return type;
  206. }
  207.  
  208. public String getSize() {
  209. return size;
  210. }
  211.  
  212. public String getWidth() {
  213. return width;
  214. }
  215.  
  216. public String getHeight() {
  217. return height;
  218. }
  219.  
  220. public String getBit_depth() {
  221. return bit_depth;
  222. }
  223. }

1.生成jar包:生成jar包时,程序不能有错误、不能有警告,编译通过后可export,我这里jar包命名为getImageInfo.jar;

2.控制台运行jar包,命令如下图:

注意:

1.路径f:\\tmpxu\\222.jpg 必须两个“\”。

2.window里切换路径和linux不同,不能直接用cd,先切换到f盘,直接输入“f:”命令,回车;

然后cd 到目标目录即可。

3.执行jar包用java -jar file.jar 命令;

最后得到色深为8;

java 读取图片色深的更多相关文章

  1. Java读取图片exif信息实现图片方向自动纠正

    起因 一个对试卷进行OCR识别需求,需要实现一个功能,一个章节下的题目图片需要上下拼接合成一张大图,起初写了一个工具实现图片的合并,程序一直很稳定的运行着,有一反馈合成的图片方向不对,起初怀疑是本身图 ...

  2. java 读取图片并转化为二进制字符串

    本例子的目的在于测试往oracle数据库中插入blob字段 //以下代码源于:https://www.cnblogs.com/ywlx/p/4544179.html public static Str ...

  3. java读取图片的(尺寸、拍摄日期、标记)等EXIF信息

    1.metadata-extractor是 处理图片EXIF信息的开源项目,最新代码及下载地址:https://github.com/drewnoakes/metadata-extractor 2.本 ...

  4. JAVA 读取图片储存至本地

    需求:serlvet经过处理通过报表工具返回一张报表图(柱状图 折线图). 现在需要把这个图存储到本地 以便随时查看 // 构造URL URL url = new URL(endStr); // 打开 ...

  5. Java读取图片并修改像素,创建图片

    public void replaceImageColor(String file, Color srcColor, Color targetColor) throws IOException{ UR ...

  6. java读取远程url图片,得到宽高

    链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100nrb6.html import java.io.IOException;import java.awt ...

  7. java读取网页图片路径并下载到本地

    java读取网页图片路径并下载到本地 最近公司需要爬取一些网页上的数据,自己就简单的写了一个demo,其中有一些数据是图片,需要下载下来到本地并且 将图片的路径保存到数据库,示例代码如下: packa ...

  8. JAVA实现读取图片

    话不读说  直接上代码 package cn.kgc.ssm.common; import java.io.*; /** * @author * @create 2019-08-15 9:36 **/ ...

  9. java IO流读取图片供前台显示

    最近项目中需要用到IO流来读取图片以提供前台页面展示,由于以前一直是用url路径的方式进行图片展示,一听说要项目要用IO流读取图片感觉好复杂一样,但任务下达下来了,做为程序员只有选择去执行喽,于是找了 ...

随机推荐

  1. BST(Binary Search Tree)

    原文链接:http://blog.csdn.net/jarily/article/details/8679280 /****************************************** ...

  2. 【BZOJ3450】【Tyvj1952】Easy 可能DP

    联系: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  3. MVC提交时验证

    第一种 @using (Html.BeginForm("ProdPromotionEdit", "Product", FormMethod.Post, new ...

  4. 管理员控制Windows Service

    C# 以管理员方式启动Winform,进而使用管理员控制Windows Service   问题起因: 1,) 问题自动分析Windows服务在正常运行时,确实会存在程序及人为原因导致该服务停止.为了 ...

  5. IOS开发计算文本尺寸

    在IOS开发中例如微博,QQ聊天界面中要显示大量的文字信息,这样需要计算出文字部分的尺寸,才能设计出合适的控件尺寸和位置.下面是IOS 7.0计算文本尺寸的方法.- (CGRect)boundingR ...

  6. 【Eclipse提高开发速度-插件篇】Eclipse插件安装慢得几个原因

    1.改动"Available Softeware Site" ,降低关联,详细做法 Install New Software >> Available Softewar ...

  7. 用bat启动sqlserver服务

    声明下这个脚本不是我写的,忘了是从哪看到的了,在此分享给大家,因为在我的理解中技术就是用来分享的,,希望原创作者看到了不要介意. 1.创建个文本,将后缀名改成.bat 2.将下边语句粘贴进去,然后保存 ...

  8. grunt的基本概念和使用

    grunt的基本概念和使用 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版 ...

  9. jmeter之自定义java请求性能测试

    一.环境准备         1.新建一个java工程         2.导入jar包:ApacheJMeter_core.jar     ApacheJMeter_java.jar         ...

  10. IOS开发之——使用SBJson拼接Json字符串

    SBJson包的下载地址在上一篇文章中. 能够使用NSDictionary中的键值对来拼接Json数据,很方便,也能够进行嵌套,直接上代码: //開始拼接Json字符串 NSDictionary *d ...