2016-04-23 16 views
0

我想读取图像,将其放大到80 * 60,然后通过复制方法使其大小复制。我的方法单独运行,但是当我用主方法调用它们时,图像变黑。任何人都可以帮助我吗? 这里是我的代码:通过回调方法缩放java中的图像

import java.awt.BorderLayout; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.RenderingHints; 
import java.awt.image.BufferedImage; 
import java.io.File; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class Test { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     BufferedImage Image = null; 
     File fc = null; 

      try{ 

       fc = new File("C:\\1.jpg"); 
       Image = ImageIO.read(fc); 
       BufferedImage zoomin = new BufferedImage(ScaledImage(Image,80,60).getWidth(null),ScaledImage(Image,80,60).getWidth(null), BufferedImage.TYPE_BYTE_GRAY); 
       JFrame frame = new JFrame("Scaled Resolution"); 
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       JLabel lbl = new JLabel(); 
       lbl.setIcon(new ImageIcon(ImgReplication(zoomin,2))); 
       frame.getContentPane().add(lbl, BorderLayout.CENTER); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 

      } 
      catch (Exception e1){ 
      System.out.println(e1); 
      } 


    } 
public static BufferedImage ImgReplication(BufferedImage image, int n) { 

     int w = n * image.getWidth(); 
     int h = n * image.getHeight(); 

     BufferedImage enlargedImage = 
       new BufferedImage(w, h, image.getType()); 

     for (int y=0; y < h; ++y) 
      for (int x=0; x < w; ++x) 
       enlargedImage.setRGB(x, y, image.getRGB(x/n, y/n)); 

     return enlargedImage; 

    } 
public static BufferedImage ScaledImage(Image img, int w , int h){ 

    BufferedImage resizedImage = new BufferedImage(w , h , BufferedImage.TYPE_BYTE_GRAY); 
    Graphics2D g2 = resizedImage.createGraphics(); 
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
    g2.drawImage(img, 0, 0, w, h, null); 
    g2.dispose(); 
    return resizedImage; 
} 


} 
+0

您是否考虑到alpha通道? –

+0

单独运作良好?你在哪一点绘制图像,效果很好? – gpasch

+0

我应该读取的图像是灰度缩放BYTE。我没有阿尔法。 – Nazi

回答

1

什么我建议是这样的:

BufferedImage Image = null; 
    File fc = null; 

     try{ 

      fc = new File("C:\\1.jpg"); 
      Image = ImageIO.read(fc); 
      BufferedImage sc=ScaledImage(Image,80,60); 
      JFrame frame = new JFrame("Scaled Resolution"); 
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
      JLabel lbl = new JLabel(); 
      lbl.setIcon(new ImageIcon(ImgReplication(sc,2))); 
      frame.getContentPane().add(lbl, BorderLayout.CENTER); 
      frame.pack(); 
      frame.setLocationRelativeTo(null); 
      frame.setVisible(true); 

     } 
     catch (Exception e1){ 
     System.out.println(e1); 
     } 


} 

public static BufferedImage ImgReplication(BufferedImage image, int n) { 

    int w = n * image.getWidth(); 
    int h = n * image.getHeight(); 

    BufferedImage enlargedImage = 
      new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 

    for (int y=0; y < h; ++y) 
     for (int x=0; x < w; ++x) 
      enlargedImage.setRGB(x, y, image.getRGB(x/n, y/n)); 

    return enlargedImage; 

} 

public static BufferedImage ScaledImage(Image img, int w , int h){ 

    BufferedImage resizedImage = new BufferedImage(w , h , BufferedImage.TYPE_INT_RGB); 
    Graphics2D g2 = resizedImage.createGraphics(); 
    g2.drawImage(img, 0, 0, w, h, null); 
    g2.dispose(); 
    return resizedImage; 
} 


} 
+0

它doesn没有什么区别,我仍然会得到黑色。我不知道我为什么得到这个。一切看起来都对我很好。谢谢你的努力。:) – Nazi

+0

你也错过了正确的参数ImgReplication(sc,2) – gpasch

+0

不,我没有错过。这里是行:lbl.setIcon(new ImageIcon(ImgReplication(sc,2)));我知道问题来自调用scaledImage方法。导致一切正常,直到击中这条使图像变黑的线。但我不明白发生了什么事。 – Nazi

1

所以, “核心” 问题就在这里......

BufferedImage zoomin = new BufferedImage(
      ScaledImage(Image,80,60).getWidth(null), 
      ScaledImage(Image,80,60).getWidth(null), 
      BufferedImage.TYPE_BYTE_GRAY); 

这一切都是正在创建一个空白图像,这是一个80x60并做了很多工作来实现它。

相反,你应该只使用类似...

BufferedImage zoomin = ScaledImage(Image,80,60); 

现在,如果你想让它灰度,那么你就需要做出同样的BufferedImage.TYPE_BYTE_GRAY类型的新BufferedImage图像尺寸为zoomin图像和油漆的zoomin像它...

你也可能想看看Quality of Image after resize very low -- JavaJava: maintaining aspect ratio of JPanel background image关于缩放图像的一些更多的想法。 get/setRGB是不是特别有效率和drawImage(x, y, w, h)质量不是很大要么

Runnable的例子...

因此,使得提出修改意见后,我跑你的算法,它想出了这个.. 。

Example

原来,ScaledImageImgReplication

import java.awt.EventQueue; 
import java.awt.Graphics2D; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Image; 
import java.awt.RenderingHints; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

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

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

        JFrame frame = new JFrame("Testing"); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.add(new TestPane()); 
        frame.pack(); 
        frame.setLocationRelativeTo(null); 
        frame.setVisible(true); 
       } catch (IOException ex) { 
        ex.printStackTrace();; 
       } 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() throws IOException { 
      BufferedImage master = ImageIO.read(new File("...")); 
      setLayout(new GridBagLayout()); 

      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 

      add(new JLabel(new ImageIcon(master)), gbc); 
      add(new JLabel(new ImageIcon(ScaledImage(master, 80, 60))), gbc); 
      add(new JLabel(new ImageIcon(ImgReplication(master, 2))), gbc); 
     } 

    } 

    public static BufferedImage ScaledImage(Image img, int w, int h) { 

     BufferedImage resizedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); 
     Graphics2D g2 = resizedImage.createGraphics(); 
     g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
     g2.drawImage(img, 0, 0, w, h, null); 
     g2.dispose(); 
     return resizedImage; 
    } 

    public static BufferedImage ImgReplication(BufferedImage image, int n) { 

     int w = n * image.getWidth(); 
     int h = n * image.getHeight(); 

     BufferedImage enlargedImage 
       = new BufferedImage(w, h, image.getType()); 

     for (int y = 0; y < h; ++y) { 
      for (int x = 0; x < w; ++x) { 
       enlargedImage.setRGB(x, y, image.getRGB(x/n, y/n)); 
      } 
     } 

     return enlargedImage; 

    } 
} 

所以它似乎为我工作

+0

我已经尝试过你提出的,但没有任何不同。仍是缩放的黑色图像。虽然我没有在寻找高质量的图像。这个程序的主要思想是比较不同的图像插值方法,看看哪一个更好的图像。 – Nazi

+0

一旦我添加了建议的修复程序,代码似乎对我来说工作得很好。 – MadProgrammer

+1

超过16年以上,我已经做了大量的图像缩放研究,试图想出快速而高效的算法[这个例子](http://stackoverflow.com/questions/14115950/quality-of-image- after-resize-very-low-java/14116752#14116752)和[this example](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928# 11959928)基于基于[Image.getScaledInstance()]的危害结果的相似原则(https://today.java.net/pub/a/today/2007/04/03/perils-of -image-getscaledinstance.html)和其他博客 – MadProgrammer