gpt4 book ai didi

Java Graphics 2D 渲染质量问题

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

使用 Java Graphics 2D 逐分钟渲染图像来创建延时视频,导致运行时图像模糊。全高清图像分辨率 (1920 X 1080) 图像而非视频上出现模糊。

我使用缓冲图像作为配置,如下所示

BufferedImage alt = config.createCompatibleImage(resX, resY);        
Graphics2D g2 = alt.createGraphics();

resXresY

final int resX = 1920;
final int resY = 1080;

尝试使用此 x,y 配置绘制圆圈和文本

// Image
g2.drawImage(img, leftSideX, resYTop + paddingImg, resX - leftSideX - paddingImg, resY - resYTop - paddingImg - paddingImg, null);

// Draw Cirlce 1
g2.setColor(new Color(0,161,155));
g2.fillArc(78, 160, 200, 200, 90, -340);

g2.setColor(new Color(73, 73, 73));
g2.fillArc(98, 180, 160, 160, 90, -340);

// Percentage
g2.setColor(new Color(0,161,155));
g2.setFont(g2.getFont().deriveFont(45F));
g2.drawString("10%", 135, 275 );

// Draw circle Legend
g2.setColor(new Color(0,161,155));
g2.fillArc(paddingLegend, resY - 150, paddingLegend, paddingLegend, 360, 360);

g2.setColor(new Color(249,178,52));
g2.fillArc(paddingLegend, resY - 120, paddingLegend, paddingLegend, 360, 360);

g2.setColor(new Color(190,22,35));
g2.fillArc(paddingLegend, resY - 90, paddingLegend, paddingLegend, 360, 360);

g2.setColor(new Color(178,178,178));
g2.setFont(g2.getFont().deriveFont( 15F ));
g2.drawString("Occupied / Moving Goods", paddingLegend + 30, resY - 135 );
g2.drawString("Goods idle > 2 days and < 5 days", paddingLegend + 30, resY - 105);
g2.drawString("Goods idle \u2265 5 days", paddingLegend + 30, resY - 75 );

// Capacity Level Title
g2.setFont(g2.getFont().deriveFont( 20F ));
g2.drawString("Capacity Level ", paddingLeftSubTitle + paddingLegend, resYTop + paddingTopSubTitle1 + paddingLegend);

// Goods Movement Title
g2.setFont(g2.getFont().deriveFont( 20F ));
g2.drawString("Goods Movement ", paddingLeftSubTitle + paddingLegend, resYTop + paddingTopSubTitle2 + paddingLegend);

这是结果
image 1

圆圈和文字出现模糊 problem 1

problem 2

problem 3

从上图中可以看到,圆圈和文字完全模糊,但仓库图像一点也不模糊。我已经尝试过以下代码,但没有任何改进。

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

如何提高图像渲染质量?

非常感谢

最佳答案

所以,当我将你的代码修补在一起并运行它时,我得到......

Nice

这对我来说似乎很好。因此,要么是您的代码中发生了我们不知道的其他问题,要么是硬件/驱动程序问题。

考虑提供Minimal, Complete, and Verifiable example - 这消除了猜测工作,为我们提供了更好的机会来理解问题并提供可能的解决方案

注意:我正在以 1920x1080 的分辨率渲染输出,我刚刚剪辑了帖子的输出

ps-我知道这不是一个“答案”,但我没有从OP那里得到任何帮助

循环进度

因为我以前还没做过一百次...

CircularProgress

虽然您可以使用常规 2D 图形 API 来完成此类操作,但我倾向于发现“形状”API 是一种更有用的方法,特别是对于生成复杂的可重复使用的形状

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CircleProgressTest {

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

public CircleProgressTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
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);
}
});
}

public class TestPane extends JPanel {

private double progress = 0d;
private double radius = 90;
private CircleProgress cp;

public TestPane() {
cp = new CircleProgress(radius, 20, 0d);
Timer timer = new Timer(50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (progress >= 1.0) {
((Timer)(e.getSource())).stop();
progress = 1.0;
} else {
progress += 0.001;
}
cp = new CircleProgress(radius, 20, progress);
repaint();
}
});
timer.start();
}

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

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

double x = (getWidth() - (radius * 2d)) / 2d;
double y = (getHeight() - (radius * 2d)) / 2d;

g2d.transform(AffineTransform.getTranslateInstance(x, y));

g2d.setStroke(new BasicStroke(4f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.setColor(Color.DARK_GRAY);
g2d.draw(cp);
g2d.setColor(new Color(0,161,155));
g2d.fill(cp);

g2d.dispose();
}

}

public class CircleProgress extends Path2D.Double {

private double radius;
private double thickness; // subtracted from radius
private double progress;

public CircleProgress(double radius, double thickness, double progress) {
this.radius = radius;
this.thickness = thickness;
this.progress = progress;

build();
}

protected void build() {
double innerRadius = radius - thickness;
if (progress >= 1.0) {
Area outter = new Area(new Ellipse2D.Double(0, 0, radius * 2d, radius * 2d));
Area inner = new Area(new Ellipse2D.Double(thickness, thickness, innerRadius * 2d, innerRadius * 2d));
outter.subtract(inner);
append(outter, true);
} else if (progress <= 0.0) {
// What does this actually mean...?
} else {
double extent = -(360.0 * progress);
double startAt = 90d;

append(new Arc2D.Double(0, 0, radius * 2d, radius * 2d, startAt, extent, Arc2D.OPEN), true);
append(new Arc2D.Double(thickness, thickness, innerRadius * 2d, innerRadius * 2d, startAt + extent, -extent, Arc2D.OPEN), true);
closePath();
}
}
}

}

关于Java Graphics 2D 渲染质量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51533231/

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