我创建了一个简单的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");
}
答案 0 :(得分:2)
在快速测试中,如果您只执行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);
答案 1 :(得分:1)
ColorAdjust
的示例,当您将鼠标悬停在图像上时,通过对其进行去饱和来将彩色图像转换为单色。
给定适当的硬件(JavaFX运行的大多数系统都可以使用),JavaFX运行时可以在硬件中执行颜色调整的一些实现,这可能比使用{{3操纵像素更有效地实现}}
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.*;
import javafx.scene.effect.*;
import javafx.scene.image.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Shadow extends Application {
@Override
public void start(Stage stage) throws Exception {
Image image = new Image(
"http://icons.iconarchive.com/icons/designbolts/smurfs-movie/512/smurfette-icon.png"
);
ImageView imageView = new ImageView(image);
ColorAdjust monochrome = new ColorAdjust();
monochrome.setSaturation(-1.0);
imageView.effectProperty().bind(
Bindings
.when(imageView.hoverProperty())
.then(monochrome)
.otherwise((ColorAdjust) null)
);
stage.setScene(new Scene(new Group(imageView), Color.AQUA));
stage.show();
}
public static void main(String[] args) {
Application.launch();
}
}