2012-02-18 6 views
0

데이터베이스에 이미지를 저장했지만 이미지를 검색하는 동안 크기를 177x122로 조정하고 싶습니다. JAVA에서 어떻게 할 수 있습니까? 다음은 데이터베이스에서 이미지를 검색하는 데 사용한 코드입니다. 177x122의 이미지를 얻으려면 어떤 변경 작업이 필요합니다.데이터베이스에서 다른 크기의 이미지 가져 오기

PreparedStatement pstm1 = con.prepareStatement("select * from image"); 
      ResultSet rs1 = pstm1.executeQuery(); 
      while(rs1.next()) { 
       InputStream fis1; 
       FileOutputStream fos; 
       String image_id; 
       try { 
        fis1 = rs1.getBinaryStream("image"); 
        image_id=rs1.getString("image_id"); 
        fos = new FileOutputStream(new File("images" + (image_id) + ".jpg")); 
        int c; 
        while ((c = fis1.read()) != -1) { 
         fos.write(c); 
        } 
        fis1.close(); 
        fos.close(); 
        JOptionPane.showMessageDialog(null, "Image Successfully Retrieved"); 

       } catch (Exception ex) { 
        System.out.println(ex); 
       } 
      } 

답변

3

AWT에서 제공하는 BufferedImage 및 Graphics2D 클래스를 사용하여 이미지의 크기를 조절할 수 있습니다. image 열의 데이터 가정 Source

BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); 
Graphics2D g = resizedImage.createGraphics(); 
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); 
g.dispose(); 
1

자바 콘텐츠가 I/O가 읽을 수있는 화상 포맷 (예컨대 JPEG 및 PNG 등)은 Thumbnailator 라이브러리이를 달성 할 수 있어야한다.

InputStreamResultSet에서 이미지 데이터를 검색하고 지정된 파일에 작성합니다 코드는 다음과 같이 쓸 수

가 :

// Get the information we need from the database. 
String imageId = rs1.getString("image_id"); 
InputStream is = rs1.getBinaryStream("image"); 

// Perform the thumbnail generation. 
// You may want to substitute variables for the hard-coded 177 and 122. 
Thumbnails.of(is) 
    .size(177, 122) 
    .toFile("images" + (imageId) + ".jpg"); 

// Thumbnailator does not automatically close InputStreams 
// (which is actually a good thing!), so we'll have to close it. 
is.close(); 

(내가 실제로이 코드를 실행하지 않은 것을 부인한다 실제 데이터베이스에 대해).

Thumbnailator가 image 열로부터 이진 데이터를 읽어 오기 InputStream로부터 화상 ​​데이터를 읽어 다음 172 X 122 영역에 맞게 영상의 크기를 조정하고, 최종적으로 출력 t 그는 지정한 파일에 JPEG로 축소판 그림을 표시합니다.

기본적으로 축소판 그림은 이미지의 크기를 조정할 때 원본 이미지의 종횡비를 보존하므로 축소판이 왜곡되지 않도록 방지해야하므로 이미지 크기가 반드시 172x122 일 필요는 없습니다.이 동작이 바람직하지 않은 경우 forceSize 메서드 대신 size 메서드를 구현할 수 있습니다.

면책 조항 : Thumbnailator 라이브러리를 유지 관리하고 있습니다.

관련 문제