彩色照片转换为黑白照片(Color image converted to black and white picture)

This blog will be talking about the color image converted to black and white picture.

The project structure as follow:

The run result:

source code:

/UUUU_Test/src/com/b510/image/clent/Client.java

 1 /**
 2  * 
 3  */
 4 package com.b510.image.clent;
 5 
 6 import java.io.File;
 7 
 8 import com.b510.image.common.Common;
 9 import com.b510.image.util.ImageUtil;
10 
11 /**
12  * @author Hongten
13  * @create 2014-7-13
14  * @mail hongtenzone@foxmail.com
15  */
16 public class Client {
17 
18     public static void main(String[] args) {
19         File input = new File(Common.ORGINAL_IMAGE);
20         File out = new File(Common.PROCESSED_IMAGE);
21         ImageUtil.changeImge(input, out);
22     }
23 }

/UUUU_Test/src/com/b510/image/common/Common.java

 1 /**
 2  * 
 3  */
 4 package com.b510.image.common;
 5 
 6 /**
 7  * @author Hongten
 8  * @create 2014-7-13
 9  * @mail hongtenzone@foxmail.com
10  */
11 public class Common {
12 
13     // Orginal image path
14     public static String ORGINAL_IMAGE = "src/com/b510/image/resources/orginal_image.jpg";
15     // Processed image path
16     public static String PROCESSED_IMAGE = "src/com/b510/image/resources/processed_image.jpg";
17 
18     public static String PROCESS_SUCCESS = "Processed successfully.....";
19     public static String PROCESS_ERROR = "Processing encounters error!";
20 }

/UUUU_Test/src/com/b510/image/util/ImageUtil.java

 1 /**
 2  * 
 3  */
 4 package com.b510.image.util;
 5 
 6 import java.awt.Image;
 7 import java.awt.color.ColorSpace;
 8 import java.awt.image.BufferedImage;
 9 import java.awt.image.ColorConvertOp;
10 import java.io.File;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 
14 import javax.imageio.ImageIO;
15 
16 import com.b510.image.common.Common;
17 import com.sun.image.codec.jpeg.JPEGCodec;
18 import com.sun.image.codec.jpeg.JPEGImageEncoder;
19 
20 /**
21  * @author Hongten
22  * @create 2014-7-13
23  * @mail hongtenzone@foxmail.com
24  */
25 public class ImageUtil {
26     /**
27      * Color image is converted to black and white picture.
28      */
29     public static void changeImge(File input, File out) {
30         try {
31             Image image = ImageIO.read(input);
32             int srcH = image.getHeight(null);
33             int srcW = image.getWidth(null);
34             BufferedImage bufferedImage = new BufferedImage(srcW, srcH, BufferedImage.TYPE_3BYTE_BGR);
35             bufferedImage.getGraphics().drawImage(image, 0, 0, srcW, srcH, null);
36             bufferedImage = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null).filter(bufferedImage, null);
37             FileOutputStream fos = new FileOutputStream(out);
38             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
39             encoder.encode(bufferedImage);
40             fos.close();
41             System.out.println(Common.PROCESS_SUCCESS);
42         } catch (IOException e) {
43             e.printStackTrace();
44             throw new IllegalStateException(Common.PROCESS_ERROR, e);
45         }
46     }
47 
48 }

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏c#开发者

Modal popup dialog window with multiple parameters

Introduction This article shows a modal popup dialog window which passes and ret...

33750
来自专栏码匠的流水账

聊聊jesque的WorkerImpl与WorkerPool

Resque是一个使用redis来创建后台任务的ruby组件。而jesque是其java版本。通常用来做延时队列。

8410
来自专栏码匠的流水账

聊聊resilience4j的bulkhead

resilience4j-bulkhead-0.13.0-sources.jar!/io/github/resilience4j/bulkhead/Bulkhe...

22310
来自专栏DannyHoo的专栏

iOS开发中利用block解决NSTimer的循环引用问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/...

15730
来自专栏函数式编程语言及工具

Akka(8): 分布式运算:Remoting-远程查找式

  Akka是一种消息驱动运算模式,它实现跨JVM程序运算的方式是通过能跨JVM的消息系统来调动分布在不同JVM上ActorSystem中的Actor进行运算,...

45990
来自专栏木宛城主

PowerShell 获取Site Collection下被签出的文件

由于权限的设置,当文件被签出时导致别人不可见了,这对校验文件个数的人来说着实是件烦恼的事。幸好利用PowerShell,可以获取Site Collection下...

20870
来自专栏非典型技术宅

iOS实践:一步步实现星级评分1. 创建星星2. 优化3. 灵异事件

19640
来自专栏用户画像

jQuery validate

8410
来自专栏码匠的流水账

聊聊rocketmq的PushConsumerImpl

io/openmessaging/rocketmq/consumer/PushConsumerImpl.java

21020
来自专栏DT乱“码”

基于springMVC拦截器实现操作日志统计

1.spring配置文件配置。  <!-- 拦截器 --> <mvc:interceptors> <!-- 日志拦截器 --> <bean cl...

26970

扫码关注云+社区

领取腾讯云代金券