java 读取图片色深
问题:
想写一个小程序可读取图片的色深(bit-depth)。网上有一些软件可完成这个功能,但是我想把程序做成一个可移植的插件。
本想用c写的,但实在麻烦,最后选择java,与很多方法不用自己写,速度快。
最后打包成一个jar包,只要装了jdk就可以在控制台运行。
我用的是MYECLIPSE,步骤如下:
1.创建一个工程;
2.创建一个java class;
3.程序包含两个类getinfo.java 和 methodclass.java;
getinfo.java包含main()方法,代码如下:
- import java.io.File;
- public class getinfo {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String url;
- if (args.length != 0) {
- url = args[0];
- } else
- url = "F:\\tmpxu\\333.png";
- File f1 = new File(url);// "F:\\tmpxu\\333.png"
- methodclass my = new methodclass();
- my.ImageInfo(f1);
- System.out.println("getName====" + my.getName());
- System.out.println("getPath====" + my.getPath());
- System.out.println("getDate_created====" + my.getDate_created());
- System.out.println("getDate_modified====" + my.getDate_modified());
- System.out.println("getType====" + my.getType());
- System.out.println("getSize====" + my.getSize());
- System.out.println("getWidth====" + my.getWidth());
- System.out.println("getHeight====" + my.getHeight());
- System.out.println("getBit_depth====" + my.getBit_depth());
- }
- }
这里注意:
控制台命令: 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,整理后的代码如下:
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.StringTokenizer;
- import javax.imageio.ImageIO;
- public class methodclass {
- private String name = "";
- private String path = "";
- private String date_created = "";
- private String date_modified = "";
- private String bytes = "";
- private String type = "";
- private String size = "";
- private String width = "";
- private String height = "";
- private String bit_depth = "";
- public void ImageInfo(File file) {
- name = file.getName();
- path = file.getParent();
- date_created = getDate_created(file);
- date_modified = new SimpleDateFormat("yyyy/MM/dd HH:mm")
- .format(new Date(file.lastModified()));
- bytes = getBytes(file);
- getImageData(file);
- getImageFileData(file);
- }
- // 判读图片类型
- private void getImageFileData(File file) {
- try {
- FileInputStream input = new FileInputStream(file);
- /*
- * he java.io.FileInputStream.available() method returns number of
- * remaining bytes that can be read from this input stream without
- * blocking by the next method call for this input stream. The next
- * method call can also be the another thread.
- * 通过available方法取得流的最大字符数
- */
- byte[] b = new byte[input.available()];
- if (b.length == 0) {
- System.out.print("the file is empty!!!");
- return;
- }
- input.read(b);
- input.close();
- int b1 = b[0] & 0xff;
- int b2 = b[1] & 0xff;
- if (b1 == 0x42 && b2 == 0x4d) {
- checkBmp(b);
- } else if (b1 == 0x47 && b2 == 0x49) {
- checkGif(b);
- } else if (b1 == 0x89 && b2 == 0x50) {
- checkPng(b);
- } else if (b1 == 0xff && b2 == 0xd8) {
- checkJpeg(b, file);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // 获得图片宽高
- private void getImageData(File file) {
- try {
- BufferedImage img = ImageIO.read(file);
- size = img.getWidth() + " x " + img.getHeight();
- width = img.getWidth() + " 像素";
- height = img.getHeight() + " 像素";
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void checkBmp(byte[] b) {
- type = "BMP 文件";
- int bitsPerPixel = (b[28] & 0xff) | (b[29] & 0xff) << 8;
- if (bitsPerPixel == 1 || bitsPerPixel == 4 || bitsPerPixel == 8
- || bitsPerPixel == 16 || bitsPerPixel == 24
- || bitsPerPixel == 32) {
- bit_depth = bitsPerPixel + "";
- }
- }
- public void checkGif(byte[] b) {
- type = "GIF 文件";
- bit_depth = (b[10] & 0x07) + 1 + "";
- }
- public void checkPng(byte[] b) {
- type = "PNG 图像";
- int bitsPerPixel = b[24] & 0xff;
- if ((b[25] & 0xff) == 2) {
- bitsPerPixel *= 3;
- } else if ((b[25] & 0xff) == 6) {
- bitsPerPixel *= 4;
- }
- bit_depth = bitsPerPixel + "";
- }
- /*
- * (b[i] & 0xff):byte转换int时的运算 其原因在于:1.byte的大小为8bits而int的大小为32bits;
- * 2.java的二进制采用的是补码形式;
- * 如果不进行&0xff,那么当一个byte会转换成int时,由于int是32位,而byte只有8位这时会进行补位,
- * 例如补码11111111的十进制数为-1转换为int时变为32个1!和0xff相与后,高24比特就会被清0了,结果就对了。 bit_depth:
- * a 1 bit image, can only show two colors, black and white. That is because
- * the 1 bit can only store one of two values, 0 (white) and 1 (black). An 8
- * bit image can store 256 possible colors, while a 24 bit image can display
- * about 16 million colors.
- */
- public void checkJpeg(byte[] b, File file) {
- type = "JPEG 图像";
- int i = 2;
- while (true) {
- int marker = (b[i] & 0xff) << 8 | (b[i + 1] & 0xff);
- int size = (b[i + 2] & 0xff) << 8 | (b[i + 3] & 0xff);
- if (marker >= 0xffc0 && marker <= 0xffcf && marker != 0xffc4
- && marker != 0xffc8) {
- bit_depth = (b[i + 4] & 0xff) * (b[i + 9] & 0xff) + "";
- break;
- } else {
- i += size + 2;
- }
- }
- }
- // 文件创建日期
- private String getDate_created(File file) {
- try {
- Process ls_proc = Runtime.getRuntime().exec(
- "cmd.exe /c dir \"" + file.getAbsolutePath() + "\" /tc");
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- ls_proc.getInputStream()));
- for (int i = 0; i < 5; i++) {
- reader.readLine();
- }
- StringTokenizer st = new StringTokenizer(reader.readLine());
- String date = st.nextToken();
- String time = st.nextToken();
- reader.close();
- return date + " " + time;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "";
- }
- // 读取文件大小
- private String getBytes(File file) {
- DecimalFormat df = new DecimalFormat();
- float length = (float) file.length();
- int p = 0;
- while (length > 1023) {
- length /= 1024;
- p++;
- }
- if (length > 99) {
- df.setMaximumFractionDigits(0);
- } else if (length > 9) {
- df.setMaximumFractionDigits(1);
- } else {
- df.setMaximumFractionDigits(2);
- }
- if (p == 0) {
- return (int) length + " 字节";
- } else if (p == 1) {
- return df.format(length) + " KB";
- } else if (p == 2) {
- return df.format(length) + " MB";
- } else if (p == 3) {
- return df.format(length) + " GB";
- } else {
- return df.format(length) + " TB";
- }
- }
- public String getName() {
- return name;
- }
- public String getPath() {
- return path;
- }
- public String getDate_created() {
- return date_created;
- }
- public String getDate_modified() {
- return date_modified;
- }
- public String getBytes() {
- return bytes;
- }
- public String getType() {
- return type;
- }
- public String getSize() {
- return size;
- }
- public String getWidth() {
- return width;
- }
- public String getHeight() {
- return height;
- }
- public String getBit_depth() {
- return bit_depth;
- }
- }
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 读取图片色深的更多相关文章
- Java读取图片exif信息实现图片方向自动纠正
起因 一个对试卷进行OCR识别需求,需要实现一个功能,一个章节下的题目图片需要上下拼接合成一张大图,起初写了一个工具实现图片的合并,程序一直很稳定的运行着,有一反馈合成的图片方向不对,起初怀疑是本身图 ...
- java 读取图片并转化为二进制字符串
本例子的目的在于测试往oracle数据库中插入blob字段 //以下代码源于:https://www.cnblogs.com/ywlx/p/4544179.html public static Str ...
- java读取图片的(尺寸、拍摄日期、标记)等EXIF信息
1.metadata-extractor是 处理图片EXIF信息的开源项目,最新代码及下载地址:https://github.com/drewnoakes/metadata-extractor 2.本 ...
- JAVA 读取图片储存至本地
需求:serlvet经过处理通过报表工具返回一张报表图(柱状图 折线图). 现在需要把这个图存储到本地 以便随时查看 // 构造URL URL url = new URL(endStr); // 打开 ...
- Java读取图片并修改像素,创建图片
public void replaceImageColor(String file, Color srcColor, Color targetColor) throws IOException{ UR ...
- java读取远程url图片,得到宽高
链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100nrb6.html import java.io.IOException;import java.awt ...
- java读取网页图片路径并下载到本地
java读取网页图片路径并下载到本地 最近公司需要爬取一些网页上的数据,自己就简单的写了一个demo,其中有一些数据是图片,需要下载下来到本地并且 将图片的路径保存到数据库,示例代码如下: packa ...
- JAVA实现读取图片
话不读说 直接上代码 package cn.kgc.ssm.common; import java.io.*; /** * @author * @create 2019-08-15 9:36 **/ ...
- java IO流读取图片供前台显示
最近项目中需要用到IO流来读取图片以提供前台页面展示,由于以前一直是用url路径的方式进行图片展示,一听说要项目要用IO流读取图片感觉好复杂一样,但任务下达下来了,做为程序员只有选择去执行喽,于是找了 ...
随机推荐
- BST(Binary Search Tree)
原文链接:http://blog.csdn.net/jarily/article/details/8679280 /****************************************** ...
- 【BZOJ3450】【Tyvj1952】Easy 可能DP
联系: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...
- MVC提交时验证
第一种 @using (Html.BeginForm("ProdPromotionEdit", "Product", FormMethod.Post, new ...
- 管理员控制Windows Service
C# 以管理员方式启动Winform,进而使用管理员控制Windows Service 问题起因: 1,) 问题自动分析Windows服务在正常运行时,确实会存在程序及人为原因导致该服务停止.为了 ...
- IOS开发计算文本尺寸
在IOS开发中例如微博,QQ聊天界面中要显示大量的文字信息,这样需要计算出文字部分的尺寸,才能设计出合适的控件尺寸和位置.下面是IOS 7.0计算文本尺寸的方法.- (CGRect)boundingR ...
- 【Eclipse提高开发速度-插件篇】Eclipse插件安装慢得几个原因
1.改动"Available Softeware Site" ,降低关联,详细做法 Install New Software >> Available Softewar ...
- 用bat启动sqlserver服务
声明下这个脚本不是我写的,忘了是从哪看到的了,在此分享给大家,因为在我的理解中技术就是用来分享的,,希望原创作者看到了不要介意. 1.创建个文本,将后缀名改成.bat 2.将下边语句粘贴进去,然后保存 ...
- grunt的基本概念和使用
grunt的基本概念和使用 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版 ...
- jmeter之自定义java请求性能测试
一.环境准备 1.新建一个java工程 2.导入jar包:ApacheJMeter_core.jar ApacheJMeter_java.jar ...
- IOS开发之——使用SBJson拼接Json字符串
SBJson包的下载地址在上一篇文章中. 能够使用NSDictionary中的键值对来拼接Json数据,很方便,也能够进行嵌套,直接上代码: //開始拼接Json字符串 NSDictionary *d ...