cool hit counter Convert color photos to black and white (colorimageconvertedtoblackandwhitepicture)_Intefrankly

Convert color photos to black and white (colorimageconvertedtoblackandwhitepicture)


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


Recommended>>
1、A full knowledge kit for you
2、Can autopilot actually be a thing or not Changan told me so
3、Face detection using Python and dlib
4、Charging like this is dangerous and can steal all the information from your phone in minutes
5、Baidu Cloud Intelligence Summit 2018 in Shenzhen

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号