자바 이미지 사이즈 변경하는 소스
( 출처: http://invincure.tistory.com/entry/JAVA-이미지-사이즈-바꾸기-이미지-리사이즈 [Do you want it? Come get it.])
메인함수로 작동되는 깔끔한 버전이네요..
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
public class ImageResizing {
public static Image resizing(Image soruce, int width, int height)
throws Exception {
int newW = width;
int newH = height;
return soruce.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
}
public static void main(String[] args) {
String imgResdFile = "c:\\a.jpg";
String imgWriteFile = "c:\\a1.jpg";
try {
// img = ImageIO.read( new File(fName));
FileInputStream fis = new FileInputStream(imgResdFile);
byte[] data = new byte[fis.available()];
System.out.println(fis.available());
fis.read(data);
fis.close();
Image image = Toolkit.getDefaultToolkit().createImage(data);
Image rtnImage = resizing(image, 640, 480);
MediaTracker tracker = new MediaTracker(new java.awt.Frame());
tracker.addImage(rtnImage, 0);
tracker.waitForAll();
BufferedImage bi = new BufferedImage(rtnImage.getWidth(null),
rtnImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawImage(rtnImage, 0, 0, null);
g.dispose();
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(bi, "jpeg", bas);
byte[] writeData = bas.toByteArray();
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(imgWriteFile)));
dos.write(writeData);
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
위의 소스를 메소드화 해서 사용하신다면 main 메소드를 ImgChange (String orgFile , String chgFile) 형태로
바꿔서 사용하시면 됩니다. 이미지 리사이즈는 해보니 시간이 건당 10초 내외의 시간이 걸리는 듯 합니다. 흠..
public static Image resizing(Image soruce, int width, int height)
throws Exception {
int newW = width;
int newH = height;
return soruce.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
}
public void ImgChange (String orgFile , String chgFile) {
String imgResdFile = orgFile ; // "c:\\a.jpg"; 경로가포함된 대상이름
String imgWriteFile = chgFile ; // "c:\\a1.jpg"; 경로가포함된 결과이름
try {
// img = ImageIO.read( new File(fName));
FileInputStream fis = new FileInputStream(imgResdFile);
byte[] data = new byte[fis.available()];
System.out.println(fis.available());
fis.read(data);
fis.close();
Image image = Toolkit.getDefaultToolkit().createImage(data);
Image rtnImage = resizing(image, 640, 480);
MediaTracker tracker = new MediaTracker(new java.awt.Frame());
tracker.addImage(rtnImage, 0);
tracker.waitForAll();
BufferedImage bi = new BufferedImage(rtnImage.getWidth(null),
rtnImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawImage(rtnImage, 0, 0, null);
g.dispose();
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(bi, "jpeg", bas);
byte[] writeData = bas.toByteArray();
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(imgWriteFile)));
dos.write(writeData);
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
'프로그램 > JAVA' 카테고리의 다른 글
JAVA 버전별 다운로드 (0) | 2021.03.09 |
---|---|
spring 프레임워크 Injection of autowired dependencies failed , could not autowire field 에러 (0) | 2020.06.26 |
MultipartRequest 실행시 에러 Corrupt form data: premature ending (0) | 2017.11.16 |
org/eclipse/core/resources/IContainer (0) | 2014.08.11 |
java.math.BigDecimal incompatible with java.lang.String (0) | 2014.07.31 |