使用Java缩小图像大小并将其保存在MySQL数据库中



我知道我们可以使用BLOB数据类型将图像保存在mysql中,我使用的代码如下,

    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();
    }

现在,我需要做什么来确保保存到数据库中的文件大小不应超过100KB,如果它超过了这个大小,我需要一些方法将图像的大小压缩到100KB。请给出你有价值的建议。

试试这个

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;
    }

也许您可以在将图像插入数据库之前先尝试压缩图像。这是我得到的压缩图像的例子:

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();

最新更新