2014-11-14 70 views
1

假设我有一个JPanelpanel,并且已经有一个paintComponent,我画了一些像矩形的形状。我可以得到一个缓冲的图像我的面板一样的:从JPanel获取高分辨率图像

int w = panel.getWidth(); 
    int h = panel.getHeight(); 
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
    Graphics2D g = bi.createGraphics(); 
    panel.paint(g); 
    ImageIO.write(bi1, "png", new File("test.png")); 

我怎样才能获得高分辨率图像从我panel的? (当前输出图像仅基于我的显示器中的我的JPanel的分辨率)

我试过AffineTransform,但它没有做我所需要的。

请注意,我已经画出了我的panel中的所有内容,并且不想在我的paintComponent方法中更改我的绘图。

我真的在寻找一种实用的方法,并感谢任何帮助。

+1

首先,使用'print'或'printAll',而不是'paint' – MadProgrammer

+0

定义“高分辨率” – MadProgrammer

+0

*“我试过'AffineTransform',但它不会做什么 我需要。“*如果它没有做你所需要的,那么我不明白你需要什么。 –

回答

8

有可能会实现这一目标的两种可能的方式,你可以调整身体的组成部分,或者你可以申请一个AffineTransformGraphics背景下,也有既...

所以,从这个屏幕的问题。 ..

Default

背景图像是相当大的下手,并已缩小(约),以适应当前组件内,这是什么即将降临重要...

我再由4调整它(这个应该,大概,让你周围300DPI)

Dimension original = tp.getSize(); 

System.out.println("Original = " + original); 

int width = tp.getWidth() * 4; 
int height = tp.getHeight() * 4; 

System.out.println("Target = " + width + "x" + height); 
tp.setSize(width, height); 
tp.doLayout(); 
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
Graphics2D g2d = img.createGraphics(); 
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); 
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); 
tp.print(g2d); 
g2d.dispose(); 

tp.setSize(original); 
tp.doLayout(); 

try { 
    ImageIO.write(img, "png", new File("Test.png")); 
} catch (IOException ex) { 
    ex.printStackTrace(); 
} 

这就造成了...

enter image description here

好吧,好吧,背景图像看起来很酷,但文字是关闭...

好了,相反,我们可以使用一个AffineTransform ...

int width = tp.getWidth() * 4; 
int height = tp.getHeight() * 4; 

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
Graphics2D g2d = img.createGraphics(); 
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); 
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); 
g2d.setTransform(AffineTransform.getScaleInstance(4, 4)); 
tp.print(g2d); 
g2d.dispose(); 

try { 
    ImageIO.write(img, "png", new File("Test.png")); 
} catch (IOException ex) { 
    ex.printStackTrace(); 
} 

这就造成了...

enter image description here

好了,现在图像看起来废话,文字是好的...


以供参考,测试代码.. 。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridBagLayout; 
import java.awt.Image; 
import java.awt.RenderingHints; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.AffineTransform; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class HiRes { 

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

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

       final TestPane tp = new TestPane(); 
       JButton print = new JButton("Print"); 
       print.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 

         Dimension original = tp.getSize(); 

         System.out.println("Original = " + original); 

         int width = tp.getWidth() * 4; 
         int height = tp.getHeight() * 4; 

         System.out.println("Target = " + width + "x" + height); 
         tp.setSize(width, height); 
         tp.doLayout(); 
         BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
         Graphics2D g2d = img.createGraphics(); 
         g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
         g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 
         g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); 
         g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 
         g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
         g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
         g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); 
//      g2d.setTransform(AffineTransform.getScaleInstance(4, 4)); 
         tp.print(g2d); 
         g2d.dispose(); 
         tp.setSize(original); 
         tp.doLayout(); 

         try { 
          ImageIO.write(img, "png", new File("Test.png")); 
         } catch (IOException ex) { 
          ex.printStackTrace(); 
         } 
        } 
       }); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(tp); 
       frame.add(print, BorderLayout.SOUTH); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private BufferedImage background; 

     public TestPane() { 

      setLayout(new GridBagLayout()); 

      try { 
       background = ImageIO.read(new File("/some/image/some/where")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      setLayout(new GridBagLayout()); 

      JLabel label = new JLabel("This is a test"); 
      label.setForeground(Color.WHITE); 
      label.setFont(label.getFont().deriveFont(24f)); 
      add(label); 

     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (background != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       Image scaled = background; 
       if (getWidth() < getHeight()) { 
        scaled = background.getScaledInstance(getWidth(), -1, Image.SCALE_SMOOTH); 
       } else { 
        scaled = background.getScaledInstance(-1, getHeight(), Image.SCALE_SMOOTH); 
       } 
       int x = (getWidth() - scaled.getWidth(this))/2; 
       int y = (getHeight() - scaled.getHeight(this))/2; 
       g2d.drawImage(scaled, x, y, this); 
       g2d.dispose(); 
      } 
     } 

    } 

} 
+0

你真是太棒了。第二种方法完全适用于我的情况。您能否向我解释发生了什么? – Mahdi

+0

这只是一个简单的缩放比例。本质上,你创建了一个符合你想要的大小的BufferedImage(在这种情况下是4x),你可以通过对所需比例(4x)的缩放变换将'AffineTransform'应用到Graphics'上下文中,并将该组件绘制到' BufferedImage的''图形',API负责其余部分 – MadProgrammer

0
  1. 使用印刷用涂料
  2. 扩展你的组件,你的图像尺寸,然后缩放图像回目标尺寸
  3. 使用变换会让你的图像模糊,尝试扩展您的图像8倍,你会看到不同