Java CMYK图片转RGB图片(TwelveMonkeys方式)
TwelveMonkeys的使用比较简单,只要把相关的jar包加入到类路径,他的类我们基本不会用到,只要使用jdk ImageIO或其上层的接口就行了。jdk的ImageIO有自动发现功能,会自动查找相关的编解码类并使用,而不使用jdk默认的编解码类,所以使用这个库是完全无入侵的
用到两个第三方库
1、thumbnailator:https://github.com/coobird/thumbnailator
2、TwelveMonkeys:https://github.com/haraldk/TwelveMonkeys
thumbnailator是图片处理的工具类,提供了很多图片处理的便捷的方法,这样我们就不要用jdk底层的ImageIO类了
TwelveMonkeys是一个图片编解码库,支持bmp,jpeg,tiff,pnm,psd等。jdk本身也支持一些图片的处理,如jpeg,bmp,png,但是jdk的图片编解码库不是很强。
为什么需要TwelveMonkeys?我在处理jpeg图片的时候,发现用jdk自带的jpeg解析器不能解析所有的jpeg格式文件(如cmyk)。出现unsupported formate 错误,用这个库后,没有出现错误。
thumbnailator的功能有按比例缩放,固定尺寸缩放,按尺寸等比缩放,旋转,加水印,压缩图片质量。thumbnailator固定尺寸缩放有可能会造成图片变型,有的时候我们可能需要固定尺寸并等比缩放,不够的地方补上空白。它没有提供直接的功能。下面是自己写的代码
public static void reduceImg(String srcImageFile, String destImageFile, int width, int height, boolean isScale)
throws IOException { InputStream inputStream = new FileInputStream(srcImageFile);
OutputStream outputStream = new FileOutputStream(destImageFile); BufferedImage bufferedImage = ImageIO.read(inputStream);
int sWidth = bufferedImage.getWidth();
int sHeight = bufferedImage.getHeight();
int diffWidth = 0;
int diffHeight = 0;
if (isScale) {
if ((double) sWidth / width > (double) sHeight / height) {
int height2 = width * sHeight / sWidth;
diffHeight = (height - height2) / 2;
} else if ((double) sWidth / width < (double) sHeight / height) {
int width2 = height * sWidth / sHeight;
diffWidth = (width - width2) / 2;
}
}
BufferedImage nbufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
nbufferedImage.getGraphics().fillRect(0, 0, width, height);//填充整个屏幕
nbufferedImage.getGraphics().drawImage(bufferedImage, diffWidth, diffHeight, width - diffWidth * 2,
height - diffHeight * 2, null); // 绘制缩小后的图
ImageIO.write(nbufferedImage, FileUtils.getExtensionName(srcImageFile), outputStream);
outputStream.close();
inputStream.close();
}
Examples
Create a thumbnail from an image file
Thumbnails.of(new File("original.jpg"))
.size(160, 160)
.toFile(new File("thumbnail.jpg"));
In this example, the image from original.jpg
is resized, and then saved to thumbnail.jpg
.
Alternatively, Thumbnailator will accept file names as a String
. Using File
objects to specify image files is not required:
Thumbnails.of("original.jpg")
.size(160, 160)
.toFile("thumbnail.jpg");
This form can be useful when writing quick prototype code, or when Thumbnailator is being used from scripting languages.
Create a thumbnail with rotation and a watermark
Thumbnails.of(new File("original.jpg"))
.size(160, 160)
.rotate(90)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("watermark.png")), 0.5f)
.outputQuality(0.8)
.toFile(new File("image-with-watermark.jpg"));
In this example, the image from original.jpg
is resized, then rotated to clockwise by 90 degrees, then a watermark is placed at the bottom right-hand corner which is half transparent, then is saved to image-with-watermark.jpg
with 80% compression quality settings.
Create a thumbnail and write to an OutputStream
OutputStream os = ...;
Thumbnails.of("large-picture.jpg")
.size(200, 200)
.outputFormat("png")
.toOutputStream(os);
In this example, an image from the file large-picture.jpg
is resized to a maximum dimension of 200 x 200 (maintaining the aspect ratio of the original image) and writes the that to the specified OutputStream
as a PNG image.
Creating fixed-size thumbnails
BufferedImage originalImage = ImageIO.read(new File("original.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.asBufferedImage();
The above code takes an image in originalImage
and creates a 200 pixel by 200 pixel thumbnail using and stores the result in thumbnail
.
Scaling an image by a given factor
BufferedImage originalImage = ImageIO.read(new File("original.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.scale(0.25)
.asBufferedImage();
The above code takes the image in originalImage
and creates a thumbnail that is 25% of the original image, and uses the default scaling technique in order to make the thumbnail which is stored in thumbnail
.
Rotating an image when creating a thumbnail
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.rotate(90)
.asBufferedImage();
The above code takes the original image and creates a thumbnail which is rotated clockwise by 90 degrees.
Creating a thumbnail with a watermark
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.5f)
.asBufferedImage();
As shown, a watermark can be added to an thumbnail by calling the watermark
method.
The positioning can be selected from the Positions
enum.
The opaqueness (or conversely, transparency) of the thumbnail can be adjusted by changing the last argument, where 0.0f
being the thumbnail is completely transparent, and 1.0f
being the watermark is completely opaque.
Writing thumbnails to a specific directory
File destinationDir = new File("path/to/output");
Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg")
.size(200, 200)
.toFiles(destinationDir, Rename.PREFIX_DOT_THUMBNAIL);
This example will take the source images, and write the thumbnails them as files to destinationDir
(path/to/output
directory) while renaming them with thumbnail.
prepended to the file names.
Therefore, the thumbnails will be written as files in:
path/to/output/thumbnail.apple.jpg
path/to/output/thumbnail.banana.jpg
path/to/output/thumbnail.cherry.jpg
It's also possible to preserve the original filename while writing to a specified directory:
File destinationDir = new File("path/to/output");
Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg")
.size(200, 200)
.toFiles(destinationDir, Rename.NO_CHANGE);
In the above code, the thumbnails will be written to:
path/to/output/apple.jpg
path/to/output/banana.jpg
path/to/output/cherry.jpg
Examples
Create a thumbnail from an image file
Thumbnails.of(new File("original.jpg"))
.size(160, 160)
.toFile(new File("thumbnail.jpg"));
In this example, the image from original.jpg
is resized, and then saved to thumbnail.jpg
.
Alternatively, Thumbnailator will accept file names as a String
. Using File
objects to specify image files is not required:
Thumbnails.of("original.jpg")
.size(160, 160)
.toFile("thumbnail.jpg");
This form can be useful when writing quick prototype code, or when Thumbnailator is being used from scripting languages.
Create a thumbnail with rotation and a watermark
Thumbnails.of(new File("original.jpg"))
.size(160, 160)
.rotate(90)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("watermark.png")), 0.5f)
.outputQuality(0.8)
.toFile(new File("image-with-watermark.jpg"));
In this example, the image from original.jpg
is resized, then rotated to clockwise by 90 degrees, then a watermark is placed at the bottom right-hand corner which is half transparent, then is saved to image-with-watermark.jpg
with 80% compression quality settings.
Create a thumbnail and write to an OutputStream
OutputStream os = ...;
Thumbnails.of("large-picture.jpg")
.size(200, 200)
.outputFormat("png")
.toOutputStream(os);
In this example, an image from the file large-picture.jpg
is resized to a maximum dimension of 200 x 200 (maintaining the aspect ratio of the original image) and writes the that to the specified OutputStream
as a PNG image.
Creating fixed-size thumbnails
BufferedImage originalImage = ImageIO.read(new File("original.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.asBufferedImage();
The above code takes an image in originalImage
and creates a 200 pixel by 200 pixel thumbnail using and stores the result in thumbnail
.
Scaling an image by a given factor
BufferedImage originalImage = ImageIO.read(new File("original.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.scale(0.25)
.asBufferedImage();
The above code takes the image in originalImage
and creates a thumbnail that is 25% of the original image, and uses the default scaling technique in order to make the thumbnail which is stored in thumbnail
.
Rotating an image when creating a thumbnail
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.rotate(90)
.asBufferedImage();
The above code takes the original image and creates a thumbnail which is rotated clockwise by 90 degrees.
Creating a thumbnail with a watermark
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.5f)
.asBufferedImage();
As shown, a watermark can be added to an thumbnail by calling the watermark
method.
The positioning can be selected from the Positions
enum.
The opaqueness (or conversely, transparency) of the thumbnail can be adjusted by changing the last argument, where 0.0f
being the thumbnail is completely transparent, and 1.0f
being the watermark is completely opaque.
Writing thumbnails to a specific directory
File destinationDir = new File("path/to/output");
Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg")
.size(200, 200)
.toFiles(destinationDir, Rename.PREFIX_DOT_THUMBNAIL);
This example will take the source images, and write the thumbnails them as files to destinationDir
(path/to/output
directory) while renaming them with thumbnail.
prepended to the file names.
Therefore, the thumbnails will be written as files in:
path/to/output/thumbnail.apple.jpg
path/to/output/thumbnail.banana.jpg
path/to/output/thumbnail.cherry.jpg
It's also possible to preserve the original filename while writing to a specified directory:
File destinationDir = new File("path/to/output");
Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg")
.size(200, 200)
.toFiles(destinationDir, Rename.NO_CHANGE);
In the above code, the thumbnails will be written to:
path/to/output/apple.jpg
path/to/output/banana.jpg
path/to/output/cherry.jpg
Java CMYK图片转RGB图片(TwelveMonkeys方式)的更多相关文章
- 上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片
原文:上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片 我们知道,如果网站上传图片时,如果用户上传的是CMYK图片,那么在网站上将是无法显示的,通常的现象是出现一个红叉.下面使用将Ima ...
- Java 图片提取RGB数组 RGBOfCharMaps (整理)
package demo; /** * Java 图片提取RGB数组 RGBOfCharMaps (整理) * 声明: * 和ImageCombining配合使用的工具,这里是提取图片的R.G.B生成 ...
- Atitit java 二维码识别 图片识别
Atitit java 二维码识别 图片识别 1.1. 解码11.2. 首先,我们先说一下二维码一共有40个尺寸.官方叫版本Version.11.3. 二维码的样例:21.4. 定位图案21.5. 数 ...
- Java和Android Http连接程序:使用java.net.URL 下载服务器图片到客户端
Java和Android Http连接程序:使用java.net.URL 下载服务器图片到客户端 本博客前面博文中利用org.apache.http包中API进行Android客户端HTTP连接的例子 ...
- java关于图片处理修改图片大小
最近做了一个关于图片浏览的内容.因为图片都是一些证件的资料的扫描件所以比较大,对系统的影响也是非常之大的,有很大可能直接把系统干死.那么我是这么处理的,给大家分享一下.如果大家有好的方案的话一定要早点 ...
- Java图片上查找图片算法
之前用按键精灵写过一些游戏辅助,里面有个函数叫FindPic,就是在屏幕范围查找给定的一张图片,返回查找到的坐标位置. 现在,Java来实现这个函数类似的功能. 算法描述: 屏幕截图,得到图A,(查找 ...
- java判断文件是否为图片
/** * 判断文件是否为图片<br> * <br> * @param pInput 文件名<br> * @param pImgeFlag 判断具体文件类型< ...
- Kotlin/Java Base64编码和解码(图片、文件)
原文: Kotlin/Java Base64编码和解码(图片.文件) | Stars-One的杂货小窝 最近在项目中使用到了Base64编码和解码,便是稍微写篇文章记录一下 PS:本文代码都是使用Ko ...
- java Html2Image 实现html转图片功能
//java Html2Image 实现html转图片功能 // html2image HtmlImageGenerator imageGenerator = new HtmlImageGenera ...
随机推荐
- java Set(集合)
set不保存重复的元素(至于如何判断元素相同则较为复杂,后面将会看到).Set中最常被使用的是测试归属表,你可以很容易地询问某个对象是否在某个Set中,正因如此,查找就成了Set最重要的操作,因此通常 ...
- 【转】卖萌的大牛你桑不起啊 ——记CVPR2011一篇极品文章
来源:http://blog.renren.com/share/228707015/7197269922 作者 : 庞宇 CVPR2011正在如火如荼的进行中,在网上能看到的部分文章中,我终于找到一篇 ...
- HTTP 和 HTTPS 的异同
什么是 HTTPS? HTTPS (基于安全套接字层的超文本传输协议 或者是 HTTP over SSL) 是一个 Netscape 开发的 Web 协议. 你也可以说:HTTPS = HTTP + ...
- 生成和打上patch的方法(转载)
原文链接:http://my.oschina.net/fgq611/blog/180750 在团队开发的过程中,经常需要生成patch,或者打上别人提供的patch,那么一般情况是如何操作的呢. 首先 ...
- 关于x509、crt、cer、key、csr、pem、der、ssl、tls 、openssl等
关于x509.crt.cer.key.csr.pem.der.ssl.tls .openssl等 TLS:传输层安全协议 Transport Layer Security的缩写 TLS是传输层安全协议 ...
- 【LOJ】#2073. 「JSOI2016」扭动的回文串
题解 就是一个回文串拼上左右两端 类似二分找lcp这么做 可以直接用哈希找回文串 注意要找A串前半部分,B串找后半部分 代码 #include <bits/stdc++.h> #defin ...
- 7-11Zombie's Treasure Chest uva12325
题意 你有一个体积为N的箱子和两种数量无限的宝物 宝物1的体积为s1 价值为v1 宝物2同理 输入均为32位带符号整数 你的任务是计算最多能带走多少价值的宝物 暴力枚举: 首先明白一点 ...
- 014 再次整理关于hadoop中yarn的原理及运行
一:对yarn的理解 1.关于yarn的组成 大约分成主要的四个. Resourcemanager,Nodemanager,Applicationmaster,container 2.Resource ...
- 应用Mongoose开发MongoDB(3)控制器(controllers)
控制器的基本构成与如何通过路由调用 控制器中通过建立函数并导出,实现前端对数据库的查询.新建.删除与修改的需求,并使之可以在路由中调用,完成API的封装.本文着重于结构之间的关系,具体问题解决方法将在 ...
- 【COGS-2638】数列操作ψ 线段树
题目链接: http://cogs.pro/cogs/problem/problem.php?pid=2638 Solution 用jry推荐的写法即可做到单次$O(log^{2}N)$,不过随机数据 ...