- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个关于调整从 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);
...但没有成功。
最佳答案
示例解决方案
该解决方案的工作原理是:
由于该解决方案混合了两个不同的工具包,因此在创建它时考虑了以下注意事项:
小心用于确保给定工具包调用使用正确的类的导入;例如,JavaFX 和 Swing/AWT 都有 Color
和 Image
类,因此有必要确保在正确的上下文中使用给定工具包的完全限定类 -将 Swing 图像直接传递给 JavaFX API 是错误的,反之亦然。
注意线程规则。 JavaFX 场景的快照必须在 JavaFX 应用程序线程上创建。 Swing API 的执行必须在 Swing 事件调度线程上进行。各个工具包的各种实用程序(例如 SwingUtilities
和 JavaFX Platform
类)用于确保满足给定工具包的线程约束。
Application
时,这是隐式完成的。类(class)。但是 Swing 应用程序不会扩展 JavaFX 应用程序类。因此,也许有点违反直觉且记录不足,JFXPanel
在使用工具包之前必须实例化以初始化JavaFX工具包。 注释
此解决方案专为满足问题的特定要求(这是一个需要进行一些颜色调整的 Swing 应用程序)而设计。如果您只想在 JavaFX 中调整图像颜色而不使用 Swing,那么 more straight-forward solutions存在并且是首选。
调用 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/
我正在尝试创建一个演示应用程序,我想在其中创建一个 ImageView ,该 ImageView 相互重叠,x 和 y 坐标略有变化(类似于所有纸牌相互重叠的任何纸牌游戏,但我们可以看到数字)。如果用
您好,我有一个要求,我需要通过拖动 ImageView 的右角来调整 ImageView 的大小和旋转 ImageView 。 我可以通过拖动 ImageView 的角成功地调整 ImageView
我目前正在尝试使用 JavaFX 在 ImageView 中居中图像。 所以我在 View 中加载图像: Image img = new Image("..."); imageView.setImag
我有一个非常简单的问题要问:我需要在屏幕右下角的 ImageView 上放一个小 Logo ,整个屏幕都很大,但我不知道如何设置坐标或如何设置说 ImageViews 处于相对位置。 像这样: 最佳答
我想像这样制作一个 ImageView 覆盖另一个 ImageView;只有一半的绿色圆圈覆盖了图像: 我试过使用 RelativeLayout 并将两个 ImageView 都放在里面。然后我使用
我们如何使 ImageView 的宽度和高度等于父 ConstraintLayout 的宽度?所以它显示一个全宽的方形 ImageView。 通常如何设置某个小部件的高度等于其他小部件的宽度? 最佳答
有点像刽子手 如果 ImageView 中有东西,如何跳过 ImageView 并检查其中是否有东西尝试检查 imageView 中是否有图像 代码 final boolean imageT
我正在用 swift 制作一种“保持在线条之间”iPhone 应用程序,我有一个移动 ImageView 和 2 个代表线条的 ImageView 。如何检测移动 ImageView 何时撞击或接触线
我正在做一个应用程序,用户通过单击图像(ImageView1)可以在另一个ImageView2中看到它。所以我尝试在变量中获取 ImageView1 的图像 BufferedImage img= Sw
我有以下看法: 如果我点击任何 ImageButton 以全屏模式显示相应的图像。例如,如果我点击 Black ,它会显示 ImageView ,如: 我还为一些图像按钮将背景设置为全屏颜色:如果我单
我在名为 DialButton2 的类中扩展了 ImageView 类(不要担心类的名称,它无关紧要)。 DialButton2 类所做的只是显示位于可绘制文件夹中的任 Intent 像。 packa
我正在使用 Xcode 8.1 和 swift 3.0 开发应用。 使用 Storyboard ,我扩展了一个 ImageView 以占据整个 View ,并为其设置了一张图片以用作背景图像(使用 s
如何将我的 imageView 裁剪成我在项目中有图像的气泡形状。 最佳答案 以下是调整大小或裁剪图像的简单代码,您只需根据需要为图像传递高度或宽度即可: 获取裁剪图像: UIImage *cropp
我有 92 张图像,我希望有一个指示符(例如复选标记)来指示图像已解锁。我在 .png 文件中有复选标记,我首先尝试的是为每个图像制作单独的副本,并将复选标记放在 Photoshop 中的图像顶部。但
我有两个 ImageView ,它们看起来像这样。我想将 TouchListener 添加到 imageview1。但听众无法正常工作。我正在将 imageview2 添加到 android:foc
我有一个列表布局,其中包含如下所示的项目: 首先有一个 ImageView(灯光),然后是两个 textView。所有这些都在 TableLayout 中。 (来源:http://code.googl
我想知道如何将 ImageView 元素添加到 JavaFx 2.1 中的区域元素。 也许我把这个元素的用法弄错了,但 AFAIK 它也是子元素的容器。 背景是我需要一个定义大小的区域,该区域应显示独
我在调整 StackPane 中的 ImageView 大小时遇到了问题:如果 StackPane 是根容器并且我将图像的高度和宽度绑定(bind)到 StackPane,那么一切都很好。
** 我有两个 ImageView ,一个用于背景,一个用于前景,我正在前景 ImageView 上绘图。对于删除功能,我这样做是因为我不想在删除时删除背景图像。它正在执行很好。现在我想保存该图像(背
我正在尝试为 watch 应用程序创建 ImageView ,我正在尝试以编程方式创建,但经典 View Controller 中使用的代码不起作用。 let imageName = "yourIma
我是一名优秀的程序员,十分优秀!