2014-08-28 90 views
0

圖像時,我想問一下您的幫助與Java SE解決以下問題誤差調整在Java

我正在開發軟件,並使用Java的圖像縮放庫,在網址:

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中的這個問題?

有沒有解決方案?

非常感謝

+0

考慮提供一個[可運行示例](https://stackoverflow.com/help/mcve),它演示了您的問題。這將導致更少的混淆和更好的響應 – MadProgrammer 2014-08-28 00:32:24

回答

0

似乎只是正常工作對我來說;縮放圖像7680x4800 @ 23.3mb JPG。我遇到的唯一問題是,從Windows資源管理器雙擊時,它將圖像放入C:\Windows\System32。您可能會考慮popuping了輸出文件的完整(CanonicalPath)一JOptionPane ...

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


    } 

} 

具有1920×1200的圖像還測試,擴展到7680x4800。

+0

通常將較大的圖像調整爲較小的作品。 ,但將大圖像調整爲更大的原因而導致程序中斷而不返回某種類型的錯誤。 – 2014-08-28 01:08:53

+0

嗯,好吧,將測試... – MadProgrammer 2014-08-28 01:09:17

+0

做一個測試1600 x 1200圖像調整大小爲6400x4800,然後創建一個.jar並從探索運行。 我測試了這個代碼並給出了相同的錯誤,請測試。 .. – 2014-08-28 01:26:44