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

时间:2014-09-26 05:53:34

标签: java

我知道我们可以使用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();
    }

现在我需要确保保存到数据库中的文件大小不超过100 KB,如果超过该大小我需要一些方法来将图像的大小压缩为100 KB.Kindly提出宝贵的建议。

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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();