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

时间:2020-12-03 16:25:38

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

The project structure as follow:

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

The run result:

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

==========================================================

source code:

==========================================================

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

 /**
*
*/
package com.b510.image.clent; import java.io.File; import com.b510.image.common.Common;
import com.b510.image.util.ImageUtil; /**
* @author Hongten
* @create 2014-7-13
* @mail hongtenzone@foxmail.com
*/
public class Client { public static void main(String[] args) {
File input = new File(Common.ORGINAL_IMAGE);
File out = new File(Common.PROCESSED_IMAGE);
ImageUtil.changeImge(input, out);
}
}

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

 /**
*
*/
package com.b510.image.common; /**
* @author Hongten
* @create 2014-7-13
* @mail hongtenzone@foxmail.com
*/
public class Common { // Orginal image path
public static String ORGINAL_IMAGE = "src/com/b510/image/resources/orginal_image.jpg";
// Processed image path
public static String PROCESSED_IMAGE = "src/com/b510/image/resources/processed_image.jpg"; public static String PROCESS_SUCCESS = "Processed successfully.....";
public static String PROCESS_ERROR = "Processing encounters error!";
}

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

 /**
*
*/
package com.b510.image.util; import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import javax.imageio.ImageIO; import com.b510.image.common.Common;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; /**
* @author Hongten
* @create 2014-7-13
* @mail hongtenzone@foxmail.com
*/
public class ImageUtil {
/**
* Color image is converted to black and white picture.
*/
public static void changeImge(File input, File out) {
try {
Image image = ImageIO.read(input);
int srcH = image.getHeight(null);
int srcW = image.getWidth(null);
BufferedImage bufferedImage = new BufferedImage(srcW, srcH, BufferedImage.TYPE_3BYTE_BGR);
bufferedImage.getGraphics().drawImage(image, 0, 0, srcW, srcH, null);
bufferedImage = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null).filter(bufferedImage, null);
FileOutputStream fos = new FileOutputStream(out);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bufferedImage);
fos.close();
System.out.println(Common.PROCESS_SUCCESS);
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException(Common.PROCESS_ERROR, e);
}
} }

========================================================

More reading,and english is important.

I'm Hongten

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

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。

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

========================================================