gpt4 book ai didi

java - 用SwingX的ImageView进行CollorAdjust?

转载 作者:行者123 更新时间:2023-12-01 22:26:08 24 4
gpt4 key购买 nike

我有一个关于调整从 swingx 库加载到 jXImageView 的图像的对比度、饱和度和色调的问题。

我有 ColorAdjust 方法。

ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(0.3);
colorAdjust.setHue(-0.03);
colorAdjust.setBrightness(0.2);
colorAdjust.setSaturation(0.2);

当用户单击“增强”按钮时,图像应该会发生一些变化,但如何做到这一点呢?请记住:我正在使用 jXImageView。

我已经通过使用以下代码增加了对比度:

    float brightenFactor = 1.5f;
BufferedImage imagem = (BufferedImage) jXImageView2.getImage();
RescaleOp op = new RescaleOp(brightenFactor, 0, null);
imagem = op.filter(imagem, imagem);
jXImageView2.updateUI();

编辑

我尝试过:

BufferedImage imagem = (BufferedImage) jXImageView2.getImage();
Image image = SwingFXUtils.toFXImage(imagem, null);//<--ERROR on that line (incompatible types: writable image cannot be converted to Image)
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(0.3);
colorAdjust.setHue(-0.03);
colorAdjust.setBrightness(0.2);
colorAdjust.setSaturation(0.2);
ImageView imageView = new ImageView(image);//<--ERROR on taht line no suitable constructor for ImageView(java.awt.Image)
imageView.setFitWidth(imagem.getWidth());
imageView.setPreserveRatio(true);
imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
jXImageView2.setImage(imagem);

...但没有成功。

最佳答案

示例解决方案

  • 左侧的图像是原始图像。
  • 右侧的图像是调整后的图像(已进行颜色去饱和以使图像变成单色)。

adjustedcolors

该解决方案的工作原理是:

  1. 转换 Swing/AWT BufferedImage进入 JavaFX Image .
  2. 使用 JavaFX ColorAdjust修改图像的效果。
  3. 一个snapshot颜色调整图像的一部分被用来创建新的 JavaFX 图像。
  4. 新的 JavaFX 镜像是 converted返回到新的 Swing/AWT BufferedImage。

由于该解决方案混合了两个不同的工具包,因此在创建它时考虑了以下注意事项:

  1. 小心用于确保给定工具包调用使用正确的类的导入;例如,JavaFX 和 Swing/AWT 都有 ColorImage 类,因此有必要确保在正确的上下文中使用给定工具包的完全限定类 -将 Swing 图像直接传递给 JavaFX API 是错误的,反之亦然。

  2. 注意线程规则。 JavaFX 场景的快照必须在 JavaFX 应用程序线程上创建。 Swing API 的执行必须在 Swing 事件调度线程上进行。各个工具包的各种实用程序(例如 SwingUtilities 和 JavaFX Platform 类)用于确保满足给定工具包的线程约束。

  3. JavaFX 工具包必须在使用前进行初始化。通常,当您的应用程序扩展 JavaFX Application 时,这是隐式完成的。类(class)。但是 Swing 应用程序不会扩展 JavaFX 应用程序类。因此,也许有点违反直觉且记录不足,JFXPanel在使用工具包之前必须实例化以初始化JavaFX工具包。

注释

  1. 此解决方案专为满足问题的特定要求(这是一个需要进行一些颜色调整的 Swing 应用程序)而设计。如果您只想在 JavaFX 中调整图像颜色而不使用 Swing,那么 more straight-forward solutions存在并且是首选。

  2. 调用 System.exit 通常足以关闭 JavaFX 工具包。示例应用程序调用 Platform.exit 来显式关闭 JavaFX 工具包,但在本例中,可能不需要显式调用 Platform.exit

这意味着解决方案中的 ColorAdjuster 可以从 Swing 程序中使用,而无需 Swing 程序显式导入任何 JavaFX 类(尽管在内部,ColorAdjuster 将导入这些类,并且系统必须满足正常的最低要求才能运行这两个类) Swing 和 JavaFX 工具包)。尽可能减少每个类的单个工具包的导入混合是可取的,因为由于潜在的名称冲突和线程相关的问题,在混合 JavaFX/Swing 应用程序的单个类中混合导入是产生繁琐错误的一个很好的来源。

ColorAdjuster.java

图像颜色调整实用程序。

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.SnapshotParameters;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;

import javax.swing.SwingUtilities;
import java.awt.image.BufferedImage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/** Uses JavaFX to adjust the color of an AWT/Swing BufferedImage */
public class ColorAdjuster {
// Instantiation of a JFXPanel is necessary otherwise the JavaFX toolkit is not initialized.
// The JFXPanel doesn't actually need to be used, instantiating it in the constructor is enough to trigger toolkit initialization.
private final JFXPanel fxPanel;

public ColorAdjuster() {
// perhaps this check is not necessary, but I feel a bit more comfortable if it is there.
if (!SwingUtilities.isEventDispatchThread()) {
throw new IllegalArgumentException(
"A ColorAdjuster must be created on the Swing Event Dispatch thread. " +
"Current thread is " + Thread.currentThread()
);
}

fxPanel = new JFXPanel();
}

/**
* Color adjustments to the buffered image are performed with parameters in the range -1.0 to 1.0
*
* @return a new BufferedImage which has colors adjusted from the original image.
**/
public BufferedImage adjustColor(
BufferedImage originalImage,
double hue,
double saturation,
double brightness,
double contrast
) throws ExecutionException, InterruptedException {
// This task will be executed on the JavaFX thread.
FutureTask<BufferedImage> conversionTask = new FutureTask<>(() -> {
// create a JavaFX color adjust effect.
final ColorAdjust monochrome = new ColorAdjust(0, -1, 0, 0);

// convert the input buffered image to a JavaFX image and load it into a JavaFX ImageView.
final ImageView imageView = new ImageView(
SwingFXUtils.toFXImage(
originalImage, null
)
);

// apply the color adjustment.
imageView.setEffect(monochrome);

// snapshot the color adjusted JavaFX image, convert it back to a Swing buffered image and return it.
SnapshotParameters snapshotParameters = new SnapshotParameters();
snapshotParameters.setFill(Color.TRANSPARENT);

return SwingFXUtils.fromFXImage(
imageView.snapshot(
snapshotParameters,
null
),
null
);
});

Platform.runLater(conversionTask);

return conversionTask.get();
}
}

ColorAdjustingSwingAppUsingJavaFX.java

测试工具:

import javafx.application.Platform;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutionException;

public class ColorAdjustingSwingAppUsingJavaFX {

private static void initAndShowGUI() {
try {
// This method is invoked on Swing thread
JFrame frame = new JFrame();

// read the original image from a URL.
URL url = new URL(
IMAGE_LOC
);
BufferedImage originalImage = ImageIO.read(url);

// use JavaFX to convert the original image to monochrome.
ColorAdjuster colorAdjuster = new ColorAdjuster();
BufferedImage monochromeImage = colorAdjuster.adjustColor(
originalImage,
0, -1, 0, 0
);

// add the original image and the converted image to the Swing frame.
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(
new JLabel(
new ImageIcon(originalImage)
)
);
frame.getContentPane().add(
new JLabel(
new ImageIcon(monochromeImage)
)
);

// set a handler to close the application on request.
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// shutdown the JavaFX runtime.
Platform.exit();

// exit the application.
System.exit(0);
}
});

// display the Swing frame.
frame.pack();
frame.setLocation(400, 300);
frame.setVisible(true);
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(
ColorAdjustingSwingAppUsingJavaFX::initAndShowGUI
);
}


// icon source: http://www.iconarchive.com/artist/aha-soft.html
// icon license: Free for non-commercial use, commercial usage: Not allowed
private static final String IMAGE_LOC =
"http://icons.iconarchive.com/icons/aha-soft/desktop-buffet/128/Pizza-icon.png";
}

关于java - 用SwingX的ImageView进行CollorAdjust?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28755243/

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