我创建了一个简单的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");
}
秋千代码:
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倍左右。与AWT Color类相比,JavaFX Color类的实现要丰富得多。我想这要付出额外的开销。int width = (int)image.getWidth();
int height = (int)image.getHeight();
int[] pixels = new int[width * height];
WritablePixelFormat 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);