http://blog.kimkoon.net/1007
파일 이미지를 다루는 방식은 인터넷에 많은데
이번에 Byte[]로 받아 처리하는 방식에 대해 정리 차원에서 기록해보자.
ibatis에서 BLOB 타입을 받아 처리하려면 설정해주어야 할 문제가 있다.
(자세한 사항은 http://wiki.dev.daewoobrenic.co.kr/mediawiki/index.php/Ibatisclobblob ,
http://blog.naver.com/PostView.nhn?blogId=mispro97&logNo=20053634922
참고)
. applicationContext.xml 소스 추가
이렇게 applicationContext.xml 파일에 추가해주어야 ibatis에서 BLOB 타입을 처리해준다고 한다.
(역시, 위의 링크와 동일하게 자세한 사항은 http://wiki.dev.daewoobrenic.co.kr/mediawiki/index.php/Ibatisclobblob ,
http://www.java2s.com/Code/Java/Spring/SetupDataSourceforMySQL.htm 참고)
더보기
2. 이미지를 byte[]로 받아 출력해 보자
@RequestMapping("/ShowImage")
public void showImage( @RequestParam("imgIdx") String imgIdx
,HttpServletResponse response
,HttpServletRequest request) throws Exception {
InputStream is = null;
byte[] bytes;
/* 저는 jpg 파일로 고정이라 이렇게 했지만 여러분은 타입을 얻어와야 한다. */
String content_type = "image/jpeg";
response.setContentType(content_type); // Content Type Set
/* DB의 BLOB 타입의 내용을 가져와서 bytes 변수에 담아보자. */
Master master = new Master();
master = MasterMgr.getMasterImg(imgIdx);
bytes = master.getBlobTypeImage();
/* String --> InputStream 타입으로 변환 */
is = new ByteArrayInputStream(bytes);
/* 이제 OutputStream 으로 출력해보자*/
ServletOutputStream os = response.getOutputStream();
int binaryRead;
while ((binaryRead = is.read()) != -1) {
os.write(binaryRead);
}
}
3. 이미지를 Resize해서 출력해 보자
( 출처 : http://blog.naver.com/PostView.nhn?blogId=ppant&logNo=70107766793 )
@RequestMapping("/ShowImage")
public void showImage( @RequestParam("imgIdx") String imgIdx
,HttpServletResponse response
,HttpServletRequest request) throws Exception {
InputStream is = null;
byte[] bytes;
/* 저는 jpg 파일로 고정이라 이렇게 했지만 여러분은 타입을 얻어와야 한다. */
String content_type = "image/jpeg";
response.setContentType(content_type); // Content Type Set
/* DB의 BLOB 타입의 내용을 가져와서 bytes 변수에 담아보자. */
Master master = new Master();
master = MasterMgr.getMasterImg(imgIdx);
bytes = master.getBlobTypeImage();
/* String --> InputStream 타입으로 변환 및 가로:100px, 비율:1 에 맞추어 Resize */
is = new ByteArrayInputStream(generateImage(bytes, 100, 1));
/* 이제 OutputStream 으로 출력해보자*/
ServletOutputStream os = response.getOutputStream();
int binaryRead;
while ((binaryRead = is.read()) != -1) {
os.write(binaryRead);
}
}
public static byte[] generateImage( byte[] imageContent
,int maxWidth
,double xyRatio) throws IOException {
BufferedImage originalImg = ImageIO.read( new ByteArrayInputStream(imageContent));
//get the center point for crop
int[] centerPoint = { originalImg.getWidth() /2, originalImg.getHeight() / 2 };
//calculate crop area
int cropWidth=originalImg.getWidth();
int cropHeight=originalImg.getHeight();
if( cropHeight > cropWidth * xyRatio ) {
//long image
cropHeight = (int) (cropWidth * xyRatio);
} else {
//wide image
cropWidth = (int) ( (float) cropHeight / xyRatio) ;
}
//set target image size
int targetWidth = cropWidth;
int targetHeight = cropHeight;
if( targetWidth > maxWidth) {
//too big image
targetWidth = maxWidth;
targetHeight = (int) (targetWidth * xyRatio);
}
//processing image
BufferedImage targetImage = new BufferedImage( targetWidth
,targetHeight
,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = targetImage.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, targetWidth, targetHeight);
graphics2D.setRenderingHint( RenderingHints.KEY_INTERPOLATION
,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage( originalImg, 0, 0
,targetWidth, targetHeight
,centerPoint[0] - (int)(cropWidth /2)
,centerPoint[1] - (int)(cropHeight /2)
,centerPoint[0] + (int)(cropWidth /2)
,centerPoint[1] + (int)(cropHeight /2)
,null);
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(targetImage, "png", output);
return output.toByteArray();
}
4. 화면 출력
<img src="/ShowImage.do?imgIdx=007 " />
댓글 없음:
댓글 쓰기