gpt4 book ai didi

java - 缩放 JPanel 的图形对象并将其保存到 BufferedImage - Java

转载 作者:行者123 更新时间:2023-12-01 10:30:25 25 4
gpt4 key购买 nike

我已经成功地将 JPanel 的打印保存到带有 ScreenImage 类的 BufferedImage 中,该类仅使用以下函数:

Panel.paint(Graphics g)

我的问题是我需要在 BufferedImage 上执行缩放,所以我失去了质量。如果我对图形对象进行缩放,我不会损失质量。这是到目前为止我的代码:

private void drawReflex(){

BufferedImage firstimg = new BufferedImage(getWidth() , getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g1 = firstimg.createGraphics();
this.paint(g1); // this method is inside a class which extends JPanel
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;
g2.scale(3,3);
drawReflex();
g2.scale(1,1);

}

不幸的是,上面的代码我没有得到任何比例。我做错了什么?

最佳答案

缩放也会产生一些级别或工件,一般来说一步缩放是个坏主意,即使在最简单的解决方案中,您也应该尝试执行 2 倍缩放操作,逐步向上/向下达到您想要的目标,诸如此类这被证明here ,您甚至可以考虑使用类似 imgscalr 的内容相反

以下示例使您能够通过使用 JSliderGraphics 上下文从 10% 缩放到 300%。

我只是以所需的缩放尺寸(基于组件的当前大小)生成 BufferedImage,缩放 Graphics 上下文,然后使用 printAll code> 生成快照。然后,它直接使用“比例”作为名称的一部分将生成的图像保存在当前中

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class ScalingTest {

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

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

ImagePane pane = new ImagePane();
JButton btn = new JButton("Scale");
JSlider slider = new JSlider(10, 300);
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(10);
slider.setSnapToTicks(true);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
float scale = slider.getValue() / 100f;
Dimension size = pane.getSize();
System.out.println("Original = " + size);
size.width *= scale;
size.height *= scale;
System.out.println("Scaled = " + size);
BufferedImage scaled = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = scaled.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.scale(scale, scale);
pane.printAll(g2d);
g2d.dispose();

ImageIO.write(scaled, "png", new File("Scaledx" + scale + ".png"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
JPanel panel = new JPanel(new GridLayout(2, 1));
panel.add(slider);
panel.add(btn);

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

public class ImagePane extends JPanel {

private BufferedImage bg;

public ImagePane() {
try {
bg = ImageIO.read(new File("/Volumes/Disk02/Dropbox/MegaTokyo/issue194.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}

@Override
public Dimension getPreferredSize() {
return bg == null ? new Dimension(200, 200) : new Dimension(bg.getWidth(), bg.getHeight());
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - bg.getWidth()) / 2;
int y = (getHeight() - bg.getHeight()) / 2;
g2d.drawImage(bg, x, y, this);
g2d.dispose();
}

}

}

就我个人而言,我会以 1:1 的比例抓取形状镜头并缩放生成的图像,因为我认为您仍然会得到更好的结果,但这就是我。您可能会发现The Perils of Image.getScaledInstance()令人感兴趣的是为什么缩放 BufferedImage 并不总是返回不错的结果

关于java - 缩放 JPanel 的图形对象并将其保存到 BufferedImage - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35121614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com