Java CMYK图片转RGB图片(TwelveMonkeys方式)

这篇具有很好参考价值的文章主要介绍了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固定尺寸缩放有可能会造成图片变型,有的时候我们可能需要固定尺寸并等比缩放,不够的地方补上空白。它没有提供直接的功能。下面是自己写的代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

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(00, width, height);//填充整个屏幕 

        nbufferedImage.getGraphics().drawImage(bufferedImage, diffWidth, diffHeight, width - diffWidth * 2,

                height - diffHeight * 2null); // 绘制缩小后的图 

        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:文章来源地址https://www.toymoban.com/news/detail-690824.html

  • 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方式)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 【Linux下6818开发板(ARM)】在液晶屏上显示RGB颜色和BMP图片

    【Linux下6818开发板(ARM)】在液晶屏上显示RGB颜色和BMP图片

    (꒪ꇴ꒪ ),hello我是 祐言 博客主页:C语言基础,Linux基础,软件配置领域博主🌍 快上🚘,一起学习! 送给读者的一句鸡汤🤔: 集中起来的意志可以击穿顽石! 作者水平很有限,如果发现错误,可在评论区指正,感谢🙏         在嵌入式系统的开发中,我们经常需要在液晶

    2024年02月08日
    浏览(11)
  • 用java语言写一个网页爬虫 用于获取图片

    以下是一个简单的Java程序,用于爬取网站上的图片并下载到本地文件夹: 这个程序首先读取指定网址的HTML源码,然后从中提取出所有的图片URL。最后,程序利用 Java 的 IO 功能下载这些图片并保存到指定的本地文件夹中。 需要注意的是,该程序只是一个简单的演示,实际使

    2024年02月11日
    浏览(14)
  • 荔枝派Zero(全志V3S)驱动开发之RGB LCD屏幕显示bmp图片

    荔枝派Zero(全志V3S)驱动开发之RGB LCD屏幕显示bmp图片

    了解 framebuffer 字符设备 了解 bmp图片格式 通过操作 /dev/fb0 字符设备来实现在 RGB LCD 屏幕上显示 bmp 图片。 显示设备例如 LCD,在 Linux 中用 Framebuffer 来表征, Framebuffer 翻译过来就是帧缓冲,简称 fb,在 /dev 目录下显示设备一般表示成这样: /dev/fbn ,应用程序通过访问这个设备

    2024年02月11日
    浏览(11)
  • 使用迭代方式解决汉诺塔问题(Java语言)

    使用迭代方式解决汉诺塔问题(Java语言)

    目录 汉诺塔问题解决 迭代介绍         在这个Java示例中,我们使用了一个 Stack 数据结构来模拟递归调用的过程。 hanoiIterative 函数接受盘子数量 n 以及三个柱子的名称作为参数,并在迭代过程中模拟汉诺塔的移动操作。 moveDisk 函数用于模拟盘子的移动操作。        

    2024年02月09日
    浏览(11)
  • Java语言之float、double内存存储方式

    Java语言之float、double内存存储方式

    目录 前言 Float  double 三.float和double对比 🎁个人主页:tq02的博客_CSDN博客-C语言,Java,Java数据结构领域博主 🎥 本文由 tq02 原创,首发于 CSDN🙉 🎄 本章讲解内容: Java的float和double的存储方式 🎁欢迎各位→ 点赞 👍 +  收藏 ⭐ +  评论 📝+ 关注 ✨ 🎥  C语言专栏 : http:/

    2024年02月13日
    浏览(17)
  • JAVA开发(腾讯云OSS图片上传)

    需求背景: 项目中需要上传图片。 存储方式: 使用腾讯云的OSS组件。 代码实现: 1、使用腾讯云OSS需要使用的参数信息: OSS的域名; OSS的地域节点; OSS存储桶的名称; OSS权限凭证; OSS权限访问秘钥; OSS图片存储策略; 使用的缩略图策略; 允许上传的图片类型; 参数

    2024年02月11日
    浏览(18)
  • Java开发或调用WebService的几种方式

    Java开发或调用WebService的几种方式

    1.服务端开发与发布 编写接口 编写接口的实现类 发布服务 访问已发布的WebService服务 打开浏览器输入http://127.0.0.1:8888/JaxWSTest?wsdl访问,如下面内容 截图内容1 浏览器中输入wsdl文档中的 http://127.0.0.1:8888/JaxWSTest?xsd=1可查看绑定的参数等信息看如下图: 截图内容2 jdk自带生成W

    2024年01月17日
    浏览(18)
  • c++ java rgb与nv21互转

    目录 jni函数 c++ rgb转nv21,可以转,不报错,但是转完只有黑白图 java yuv420保存图片,先转nv21,再保存ok: c++ yuv420月bgr互转,测试ok

    2024年02月11日
    浏览(9)
  • 《java 桌面软件开发》swing 以鼠标为中心放大缩小移动图片

    《java 桌面软件开发》swing 以鼠标为中心放大缩小移动图片

    swing 使用Graphic2D 绘制图片,要实现对图片进行缩放和自由拖动。 1.以鼠标所在的位置为中心,滚轮控制缩放 2.缩放后再支持鼠标拖动。 基本原理: 利用scale() 函数。进行缩放。但是要注意的地方是,如果是在 public void paintComponent(Graphics g) 里面通过这个Graphics g 参数获取gra

    2024年02月06日
    浏览(14)
  • JAVA开发(通过Apollo注入配置信息的几种方式)

    JAVA开发(通过Apollo注入配置信息的几种方式)

    在springCloud中有一个重要的组件就是配置中心,config:server,用于配置springboot中需要注入的各种配置项。但是现在发现越来越多的企业使用Apollo进行集成。博主在开发中也是使用Apollo进行配置。本文总结Apollo的的使用,集成到springboot,和注入方式等。   Apollo是携程框架部门研

    2024年02月09日
    浏览(13)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包