简体   繁体   English

如何使用Java将调整大小的图像保存在文件夹中?

[英]how to save resize image in a folder using java?

can anyone help me how to save my resize image in a folder? 谁能帮助我如何将调整大小的图像保存在文件夹中? i am using bufferedimage and jfilechooser. 我正在使用bufferedimage和jfilechooser。 please guys help me. 请大家帮我。

private void cmd_attachActionPerformed(java.awt.event.ActionEvent evt) {                                           
        JFileChooser ch = new JFileChooser();
        ch.showOpenDialog(null);
        File f=ch.getSelectedFile();
        filename =f.getAbsolutePath();
        path.setText(filename);
        try{
            File image = new File(filename);
            BufferedImage bufferedimage=ImageIO.read(image);
            BufferedImage thumbnail=Thumbnails.of(bufferedimage)
            .size(150,188)
            .asBufferedImage();

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(thumbnail, "jpeg", os);

            InputStream is=new ByteArrayInputStream(os.toByteArray());
            // FileInputStream fis=new FileInputStream(image);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buff = new byte[1024];

            try{
                for (int readNum; (readNum=is.read(buff ))!=-1;){
                    bos.write(buff,0,readNum);
                    System.out.println("Read" +readNum+ "bytes,");
                }
            }catch(IOException ex){
                java.util.logging.Logger.getLogger(Employee_info.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            person_name=bos.toByteArray();
        }catch(Exception e){

            JOptionPane.showMessageDialog(null, "Select an image!");
        }
        finally{
            try{
                pst.close();
                rs.close();
            }catch(Exception e){
            }
        }
        Update_table();
    }       

If you already have BufferedImage you can use Graphics2D 如果您已经有了BufferedImage ,则可以使用Graphics2D

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

or AffineTransformation to scale images AffineTransformation缩放图像

/**
 * scale image
 * 
 * @param image to scale
 * @param type type of image
 * @param dWidth width of destination image
 * @param dHeight height of destination image
 * @param fWidth x-factor for transformation / scaling
 * @param fHeight y-factor for transformation / scaling
 * @return scaled image
 */
public static BufferedImage scale(BufferedImage image, int type, int dWidth, int dHeight, double fWidth, double fHeight) {
    BufferedImage dbi = null;
    if(image != null) {
        dbi = new BufferedImage(dWidth, dHeight, type);
        Graphics2D g = dbi.createGraphics();
        AffineTransform at = AffineTransform.getScaleInstance(fWidth, fHeight);
        g.drawRenderedImage(image , at);
    }
    return dbi;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM