/*
이미지 파일을 읽어 들여 base64형태로 변경해주는 기능을 제공
*/
package org.image.util;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.codec.binary.Base64;
import javax.imageio.ImageIO;
public class Base64EncodingImage {
public static void main(String[] args) {
// TODO Auto-generated method stub
Base64EncodingImage base64EncodingImage = new Base64EncodingImage();
String sourceImageName ="ccccc.png";
String targetImageName ="ccccc.txt";
try {
base64EncodingImage.changeImage(sourceImageName, targetImageName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(base64EncodingImage.toString());
}
private final String STR_EMPTY = "";
private final String STR_DOC=".";
private BufferedImage bufferedImage;
private ByteArrayOutputStream byteArrayOutputStream;
private byte[] byteArrayImage;
private final int ONE=1;
//이미지 변화 인터페이스
public void changeImage(String sourceImageName, String targetImageName)throws Exception
{
if(checkNull(sourceImageName))
throw new Exception("[EncodingImage changeImage] sourceImageName is null");
loadImageFile(sourceImageName);
if(!checkNull(targetImageName))
filePrint(targetImageName);
else consolPrint();
}
//change to base64code
private void loadImageFile(String imageFileName) throws Exception
{
File file; //file object 생성
String postFix; //확장자 확인
if(checkNull(imageFileName))
throw new Exception("[EncodingImage loadImageFile] image path value is null");
//확장자 가져오기
postFix = imageFileName.substring(imageFileName.lastIndexOf(STR_DOC)+ONE);
try
{
//이미지를 byte Object로 출력하기위해서 필요
byteArrayOutputStream = new ByteArrayOutputStream();
file = new File(imageFileName);
//이미지를 read
bufferedImage = ImageIO.read(file);
//postFix에는 확장자를 입력해주면 ImageIO객체가 알아서 처리
ImageIO.write(bufferedImage,postFix,byteArrayOutputStream);
//byte stream Object 마무리 처리
byteArrayOutputStream.flush();
//byte array로 byte stream의 데이터를 담는 역할
byteArrayImage= Base64.encodeBase64(byteArrayOutputStream.toByteArray());
}
catch(Exception e)
{
System.out.println("[EncodingImage loadImageFile Exception]");
e.printStackTrace();
throw e;
}
finally
{
closeObject();
}
}
//toString
public String toString() {
return new String(byteArrayImage);
}
//consol print
private void consolPrint()
{
System.out.println(toString());
}
//result write in file
private void filePrint(String fileName ) throws Exception
{
if(checkNull(fileName))
throw new Exception("[EncodingImage filePrint] fileName is null");
//out put file
File file = new File(fileName);
//out stream object
OutputStream outputStream = null;
try
{
//file object load
outputStream = new FileOutputStream(file);
outputStream.write(byteArrayImage);
outputStream.flush();
}
catch(IOException e)
{
System.out.println("[EncodingImage filePrint IOException]");
e.printStackTrace();
throw e;
}
finally
{
//결과를저장하는기능
if(outputStream!=null)
{
//close
outputStream.close();
outputStream=null;
}
}
}
//close object resource
private void closeObject()
{
try
{
if(byteArrayOutputStream!=null)
byteArrayOutputStream.close();
byteArrayOutputStream=null;
}
catch(Exception e)
{
System.out.println("[EncodingImage closeObject Exception]");
e.printStackTrace();
}
}
//check value null
private Boolean checkNull(String str)
{
Boolean bool=false;
if(str == null || STR_EMPTY.equals(str.trim()))
bool = true;
return bool;
}
}
쿠버네티스 node와 pod의 차이 (0) | 2022.05.04 |
---|---|
웹 전용 서브넷 마스크[2021.10.28] (0) | 2021.10.28 |
java runtime 을 이용한 외부 명령어 실행 nslookup [2021.04.22] (0) | 2021.04.22 |
2021.03.21 ini4j 샘플 (0) | 2021.03.21 |
2021.03.18 Runnable 인터페이스 구현 과 Thread 클래스 상속의 차이 (0) | 2021.03.18 |
댓글 영역