在java中调整图像大小时出错

时间:2014-08-28 00:29:23

标签: java image image-scaling resize-image

我想请求您帮助解决以下问题

我正在使用Java SE开发软件并使用url中的Java-Image Scaling库:

https://code.google.com/p/java-image-scaling/

我要调整一张6400x4800 47 MB​​的照片。

如果我在Netbeans中运行程序,则成功执行调整大小。 如果我运行JAR启动DOS也会成功调整大小。 如果我在Windows中运行JAR文件资源管理器,图像不会调整大小,程序将永久停止。不会产生任何异常

问题在于代码行(当.JAR在Windows上运行时):

重新调整BufferedImage = resampleOp.filter(src,null);

我认为Windows锁定会调整大小,因为图像大小太大或太重或需要很长时间才能运行此大小调整。

如果调整的图像较小,则不会发生Windows错误。我做了这个测试

如何在Windows中解决此问题?

这有什么解决方案吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

似乎对我来说工作得很好;缩放图像7680x4800 @ 23.3mb JPG。我唯一遇到的问题是,当从Windows资源管理器中双击时,它将图像放在C:\Windows\System32中。您可以考虑使用输出文件的完整(JOptionPane)弹出CanonicalPath ...

import com.mortennobel.imagescaling.ProgressListener;
import com.mortennobel.imagescaling.ResampleOp;
import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    protected JLabel message;

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                message = new JLabel("Rescampling, wait for it...");
                message.setHorizontalAlignment(JLabel.CENTER);
                message.setBorder(new EmptyBorder(10, 10, 10, 10));

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(message);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                ResampleWorker worker = new ResampleWorker();
                worker.execute();
            }
        });
    }

    public class ResampleWorker extends SwingWorker<String, String> {

        @Override
        protected String doInBackground() throws Exception {
            ProgressMonitor pm = new ProgressMonitor(null, "Scaling", "Please wait...", 0, 100);
            File source = new File("C:\\Users\\shane\\Downloads\\1713601.jpg");
            try {
                System.out.println("Reading...");
                publish("Reading source image...");
                BufferedImage img = ImageIO.read(source);

                int toWidth = img.getWidth() * 10;
                int toHeight = img.getHeight()* 10;

                System.out.println("From..." + (img.getWidth()) + "x" + (img.getHeight()));
                System.out.println("to..." + toWidth + "x" + toHeight);
                publish("Resample..." + toWidth + "x" + toHeight);
                ResampleOp op = new ResampleOp(toWidth, toHeight);
                Thread.yield();
                op.addProgressListener(new ProgressListener() {
                    public void notifyProgress(float fraction) {
                        int p = (int) (fraction * 100);
                        pm.setProgress(p);
//                        System.out.println(p);
                    }
                });
                BufferedImage scaled = op.filter(img, null);
                pm.close();
//                File dest = new File("scaled.jpg");
//                ImageIO.write(scaled, "jpg", dest);
//                JTextField field = new JTextField(dest.getCanonicalPath());
//                JOptionPane.showMessageDialog(null, field);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Failed to load - " + ex.getMessage());
            }
            publish("Done...");
            return null;
        }

        @Override
        protected void process(List<String> chunks) {
            message.setText(chunks.get(chunks.size() - 1));
            message.revalidate();
            message.repaint();
        }

        @Override
        protected void done() {
            try {
                get();
                JOptionPane.showMessageDialog(null, "All done");
            } catch (InterruptedException | ExecutionException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Error " + ex.getMessage());
            }
            System.exit(0);
        }


    }

}

还测试了1920x1200的图像,缩放到7680x4800。