gpt4 book ai didi

JavaFX PixelWriter 低性能

转载 作者:行者123 更新时间:2023-11-29 05:09:16 25 4
gpt4 key购买 nike

我创建了一个简单的 JavaFX 图像编辑器。有两个 ImageView 实例(打开用于原始图像,一个用于编辑后的图像)。一种方法将 WritableImage 转换为灰度。问题是,转换花费的时间大约是 Swing 完成相同任务所需时间的两倍。知道为什么吗?

@FXML
void loadImage(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(Main.primaryStage);
if (file != null) {
Image image = new Image("file:" + file.getAbsolutePath());
imViewOrig.setImage(image);
PixelReader pr = imViewOrig.getImage().getPixelReader();
WritableImage wi = new WritableImage(
pr,
(int)imViewOrig.getImage().getWidth(),
(int)imViewOrig.getImage().getHeight());
imViewEdit.setImage(wi);
}
}

@FXML
void greyscale(ActionEvent event) {
PixelReader pr = imViewOrig.getImage().getPixelReader();
PixelWriter pw = ((WritableImage)imViewEdit.getImage()).getPixelWriter();
long beginTime = System.currentTimeMillis();
for (int x = 0; x < imViewEdit.getImage().getWidth(); x++) {
for (int y = 0; y < imViewEdit.getImage().getHeight(); y++) {
Color c = pr.getColor(x, y);
double avg = (c.getRed() + c.getGreen() + c.getBlue()) / 3.;
Color grey = new Color(avg, avg, avg, c.getOpacity());
pw.setColor(x, y, grey);
}
}
System.out.println(System.currentTimeMillis() - beginTime + "ms");
}

Swing 代码:

public void greyscale() {
long beginTime = System.currentTimeMillis();
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
Color c = new Color(image.getRGB(x, y));
int avg = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
Color grey = new Color(avg, avg, avg);
image.setRGB(x, y, grey.getRGB());
}
}
repaint();
System.out.println(System.currentTimeMillis() - beginTime + "ms");
}

最佳答案

在快速测试中,如果您只进行 int 计算,而不是操作 Color 对象,它的运行速度会提高 10 倍。 JavaFX Color 类是比 AWT Color 类更丰富的实现;我想这是以额外开销为代价的。

    int width = (int)image.getWidth();
int height = (int)image.getHeight();

int[] pixels = new int[width * height];
WritablePixelFormat<IntBuffer> pixelFormat = PixelFormat.getIntArgbPreInstance();
pr.getPixels(0, 0, width, height, pixelFormat, pixels, 0, width);

int[] newPixels = new int[pixels.length];
for (int i = 0 ; i < pixels.length; i++) {
int c = pixels[i];
int a = c & 0xFF000000 ;
int r = (c >> 16) & 0xFF ;
int g = (c >> 8) & 0xFF ;
int b = c & 0xFF ;
int gray = (r+g+b) / 3 ;
newPixels[i] = a | (gray << 16) | (gray << 8) | gray ;
}

pw.setPixels(0, 0, width, height, pixelFormat, newPixels, 0, width);

关于JavaFX PixelWriter 低性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29260147/

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