JAVA 이미지 처리 블랙 / 화이트 투명 화
package org.hdht.util.image;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class TransferProcess {
public final static int COLOR_WHITE = 0;
public final static int COLOR_BLACK = 1;
/**
*
* @param sourcePath
* @param targetPath , null
* @param type B: W:
* @return
*/
public static byte[] transferAlpha(String sourcePath,String targetPath,int color) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
File iFile = new File(sourcePath);
if(!iFile.exists()) return byteArrayOutputStream.toByteArray();
ImageIcon imageIcon = new ImageIcon(ImageIO.read(iFile));
BufferedImage bufferedImage = new BufferedImage(
imageIcon.getIconWidth(), imageIcon.getIconHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
g2D.drawImage(imageIcon.getImage(), 0, 0,imageIcon.getImageObserver());
int alpha = 0;
for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
int rgb = bufferedImage.getRGB(j2, j1);
if (checkColor(rgb,16,color)) {
rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
}
bufferedImage.setRGB(j2, j1, rgb);
}
}
g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
File targetFile = null;
if(targetPath == null){
targetFile = new File(sourcePath+"_"+color+".png");
}else{
targetFile = new File(targetPath);
if(!targetFile.exists()){
File dir = new File(targetFile.getParent());
if(!dir.exists()) dir.mkdirs();
}
}
ImageIO.write(bufferedImage, "png", targetFile);
// byte[]
//ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
} catch (Exception e) {
e.printStackTrace();
}
return byteArrayOutputStream.toByteArray();
}
/**
*
* @param rgb
* @param color 0: 1:
* @return
*/
private static boolean checkColor(int rgb,int offset,int color){
int R = (rgb & 0xff0000) >> 16;
int G = (rgb & 0xff00) >> 8;
int B = (rgb & 0xff);
if(color == 0){
return ((255 - R) <= offset) && ((255 - G) <= offset) && ((255 - B) <= offset);
}else{
return ((R <= offset) && (G <= offset) && (B <= offset));
}
}
/**
*
* @param src0i 0
* @param src1i 1
* @param out
*/
public final static void mergeImage(String src0i,String src1i,String out){
try {
File f0 = new File(src0i);
File f1 = new File(src1i);
Image srcimg0 = ImageIO.read(f0);
Image srcimg1 = ImageIO.read(f1);
System.out.println(f0.getName()+" + "+f1.getName()+" = "+out);
int width = srcimg0.getWidth(null);
int height = srcimg0.getHeight(null);
BufferedImage buffereI0 = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g0 = buffereI0.createGraphics();
g0.drawImage(srcimg0, 0, 0, width, height, null);
BufferedImage buffereI1 = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g1 = buffereI1.createGraphics();
g1.drawImage(srcimg1, 0, 0, null);
for (int j1 = buffereI0.getMinY(); j1 < buffereI0.getHeight(); j1++) {
for (int j2 = buffereI0.getMinX(); j2 < buffereI0.getWidth(); j2++) {
int rgb0 = buffereI0.getRGB(j2, j1);
int rgb1 = buffereI1.getRGB(j2, j1);
buffereI0.setRGB(j2, j1, (checkColor(rgb0,0,COLOR_WHITE) && !checkColor(rgb1,0,COLOR_WHITE))?rgb1:rgb0);
}
}
g0.dispose();
FileOutputStream fout = new FileOutputStream(out);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fout);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buffereI0);
param.setQuality(80f, true);
encoder.encode(buffereI0);
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param sources
* @param target
*/
public static void marge(String[] sources,String target){
for(int i=0;i<sources.length;i++){
if(i==0){
i++;
mergeImage(sources[0],sources[1],target);
}else{
mergeImage(target,sources[i],target);
}
}
}
public static void main(String[] args) throws IOException {
//
String[] sources = {"G:\\TBS\\FY3A_MERSI_T182_L2_PAD_MLT_GLL_20130408_2040_0250M_MS_03_02_01.JPG",
"G:\\TBS\\FY3A_MERSI_T182_L2_PAD_MLT_GLL_20130408_2045_0250M_MS_03_02_01.JPG",
"G:\\TBS\\FY3A_MERSI_T182_L2_PAD_MLT_GLL_20130408_1900_0250M_MS_03_02_01.JPG",
"G:\\TBS\\FY3A_MERSI_T182_L2_PAD_MLT_GLL_20130408_1905_0250M_MS_03_02_01.JPG"};
marge(sources,"G:\\TBS\\out4.jpg");
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.