2014-09-26 160 views
0

將其保存在MySQL數據庫我知道,我們可以使用BLOB數據類型,我已經如下使用它的代碼保存圖像在MySQL中,減小圖像的大小和與Java

JFileChooser fc = new JFileChooser(); 
    fc.setFileFilter(new JPEGImageFileFilter()); 
    int res = fc.showOpenDialog(null); 
    try { 
     if (res == JFileChooser.APPROVE_OPTION) { 

      File image = new File(fc.getSelectedFile().getPath()); 
      FileInputStream fis = new FileInputStream (image); 
      String sql="insert into imgtst (username,image) values (?, ?)"; 
      pst=con.prepareStatement(sql); 
      pst.setString(1, user); 
      pst.setBinaryStream (2, fis, (int) file.length()); 

     } else { 
      JOptionPane.showMessageDialog(null, "you must select image", 
        "Abortin", JOptionPane.WARNING_MESSAGE); 
     } 
    } catch (Exception ioException) { 
      e.printStackTrace(); 
    } 

現在我需要什麼請確保保存到數據庫中的文件大小不應超過100 KB,如果超過該大小,我需要某種方法將圖像大小壓縮爲100 KB。請仔細閱讀您的寶貴建議。

+1

你爲什麼不保存圖像文件夾中,並保存路徑2數據庫 – 2014-09-26 05:57:47

+0

http://www.javalobby.org/articles/ultimate-image/ #11 – 2014-09-26 05:59:32

+0

因爲圖像是以某種方式壓縮的,所以圖像最終的實際字節大小很難確定,因爲壓縮會在圖像中引入許多不同的變量... – MadProgrammer 2014-09-26 06:00:20

回答

1

試試這個

public static BufferedImage resizeImage(Image image, int width, int height) { 
     final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
     final Graphics2D graphics2D = bufferedImage.createGraphics(); 
     graphics2D.setComposite(AlphaComposite.Src); 
     graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
     graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY); 
     graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); 
     graphics2D.drawImage(image, 0, 0, width, height, null); 
     graphics2D.dispose(); 
     return bufferedImage; 
    } 
0

也許你可以嘗試先壓縮圖像之前將其插入到數據庫中。 這是我得到的壓縮圖像的例子:

File imageFile = new File("myimage.jpg"); 
     File compressedImageFile = new File("myimage_compressed.jpg"); 

     InputStream is = new FileInputStream(imageFile); 
     OutputStream os = new FileOutputStream(compressedImageFile); 

     float quality = 0.5f; 

     // create a BufferedImage as the result of decoding the supplied InputStream 
     BufferedImage image = ImageIO.read(is); 

     // get all image writers for JPG format 
     Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); 

     if (!writers.hasNext()) 
      throw new IllegalStateException("No writers found"); 

     ImageWriter writer = (ImageWriter) writers.next(); 
     ImageOutputStream ios = ImageIO.createImageOutputStream(os); 
     writer.setOutput(ios); 

     ImageWriteParam param = writer.getDefaultWriteParam(); 

     // compress to a given quality 
     param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 
     param.setCompressionQuality(quality); 

     // appends a complete image stream containing a single image and 
     //associated stream and image metadata and thumbnails to the output 
     writer.write(null, new IIOImage(image, null, null), param); 

     // close all streams 
     is.close(); 
     os.close(); 
     ios.close(); 
     writer.dispose(); 
+0

我從這個鏈接http://examples.javacodegeeks.com/desktop-java/imageio/compress-a-jpeg-file/ – 2014-09-26 06:07:34