java rgb cmyk_Java CMYK图片转RGB图片(TwelveMonkeys方式)
TwelveMonkeys的使用比較簡單,只要把相關的jar包加入到類路徑,他的類我們基本不會用到,只要使用jdk?ImageIO或其上層的接口就行了。jdk的ImageIO有自動發(fā)現(xiàn)功能,會自動查找相關的編解碼類并使用,而不使用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圖片的時候,發(fā)現(xiàn)用jdk自帶的jpeg解析器不能解析所有的jpeg格式文件(如cmyk)。出現(xiàn)unsupported formate 錯誤,用這個庫后,沒有出現(xiàn)錯誤。
thumbnailator的功能有按比例縮放,固定尺寸縮放,按尺寸等比縮放,旋轉,加水印,壓縮圖片質(zhì)量。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 fromoriginal.jpgis resized, and then saved tothumbnail.jpg.
Alternatively,Thumbnailatorwill accept file names as aString. UsingFileobjects 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 whenThumbnailatoris 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 fromoriginal.jpgis 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 toimage-with-watermark.jpgwith 80% compression quality settings.
Create a thumbnail and write to anOutputStream
OutputStream os = ...;
Thumbnails.of("large-picture.jpg")
.size(200, 200)
.outputFormat("png")
.toOutputStream(os);
In this example, an image from the filelarge-picture.jpgis resized to a maximum dimension of 200 x 200 (maintaining the aspect ratio of the original image) and writes the that to the specifiedOutputStreamas 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 inoriginalImageand creates a 200 pixel by 200 pixel thumbnail using and stores the result inthumbnail.
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 inoriginalImageand 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 inthumbnail.
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 thewatermarkmethod.
The positioning can be selected from thePositionsenum.
The opaqueness (or conversely, transparency) of the thumbnail can be adjusted by changing the last argument, where0.0fbeing the thumbnail is completely transparent, and1.0fbeing 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 todestinationDir(path/to/outputdirectory) while renaming them withthumbnail.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 fromoriginal.jpgis resized, and then saved tothumbnail.jpg.
Alternatively,Thumbnailatorwill accept file names as aString. UsingFileobjects 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 whenThumbnailatoris 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 fromoriginal.jpgis 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 toimage-with-watermark.jpgwith 80% compression quality settings.
Create a thumbnail and write to anOutputStream
OutputStream os = ...;
Thumbnails.of("large-picture.jpg")
.size(200, 200)
.outputFormat("png")
.toOutputStream(os);
In this example, an image from the filelarge-picture.jpgis resized to a maximum dimension of 200 x 200 (maintaining the aspect ratio of the original image) and writes the that to the specifiedOutputStreamas 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 inoriginalImageand creates a 200 pixel by 200 pixel thumbnail using and stores the result inthumbnail.
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 inoriginalImageand 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 inthumbnail.
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 thewatermarkmethod.
The positioning can be selected from thePositionsenum.
The opaqueness (or conversely, transparency) of the thumbnail can be adjusted by changing the last argument, where0.0fbeing the thumbnail is completely transparent, and1.0fbeing 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 todestinationDir(path/to/outputdirectory) while renaming them withthumbnail.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 rgb cmyk_Java CMYK图片转RGB图片(TwelveMonkeys方式)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机网络实验1 网线、配线架与机柜
- 下一篇: 电子邮件营销七大案例