简体   繁体   English

调整图像文件大小

[英]Resize an image file

In Java I am copying an image file from one location to another . 在Java中,我将图像文件从一个位置复制到另一个位置。 The program is executing properly, but I want the destination file size to be different from the source file size. 该程序执行正常,但是我希望目标文件大小与源文件大小不同。 Is there any other way to resize the file at the new location? 还有其他方法可以在新位置调整文件大小吗? I am using the following code: 我正在使用以下代码:

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }

    public static void copyFile(File sourceFile, File destFile)
            throws IOException {
        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();

            // previous code: destination.transferFrom(source, 0, source.size());
            // to avoid infinite loops, should be:
            long count = 0;
            long size = source.size();
            while ((count += destination.transferFrom(source, count, size
                    - count)) < size)
                ;
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            File sourceFile = new File(
                    "d:/adesh/golden_temple_amritsar_india-normal.jpg");

            File destFile = new File(
                    "d:/adesh2/golden_temple_amritsar_india-normal.jpg");

            copyFile(sourceFile, destFile);
        } catch (Exception ex) {
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
}

Here is the code for resizing the image as per your specification. 这是根据您的规范调整图像大小的代码。 Inside the copyFile method, 在copyFile方法中,

int width=100,height=75; int width = 100,height = 75; /* set the width and height here */ / *在此处设置宽度和高度* /

BufferedImage inputImage=ImageIO.read(sourceFile); BufferedImage inputImage = ImageIO.read(sourceFile);

BufferedImage outputImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); BufferedImage outputImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

Graphics2D g=outputImage.createGraphics(); Graphics2D g = outputImage.createGraphics();

g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);

g.clearRect(0, 0, width, height); g.clearRect(0,0,width,height);

g.drawImage(inputImage, 0, 0, width, height, null); g.drawImage(inputImage,0,0,width,height,null);

g.dispose(); g.dispose();

ImageIO.write(outputImage,"jpg",destFile); ImageIO.write(outputImage,“ jpg”,destFile); /* first parameter is the object of the BufferedImage, second parameter is the type of image that you are going to write, you can use jpg, bmp, png etc and the third parameter is the destination file object. / *第一个参数是BufferedImage的对象,第二个参数是您要编写的图像类型,可以使用jpg,bmp,png等,第三个参数是目标文件对象。 */ * /

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

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