Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Collora与SwingX和x27进行调整;什么是图像视图?_Java_Image Processing_Netbeans_Javafx_Swingx - Fatal编程技术网

Java Collora与SwingX和x27进行调整;什么是图像视图?

Java Collora与SwingX和x27进行调整;什么是图像视图?,java,image-processing,netbeans,javafx,swingx,Java,Image Processing,Netbeans,Javafx,Swingx,我有一个关于调整从swingx库加载到jXImageView的图像的对比度、饱和度和色调的问题 我有颜色调整的方法 ColorAdjust colorAdjust = new ColorAdjust(); colorAdjust.setContrast(0.3); colorAdjust.setHue(-0.03); colorAdjust.setBrightness(0.2); colorAdjust.setSaturation(0.2); 当用户点击“增强”按钮时,图像应该会有一点变化,但

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

我有颜色调整的方法

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);
buffereImage imagem=(buffereImage)jXImageView2.getImage();

Image Image=SwingFXUtils.toFXImage(imagem,null)// 您需要将
buffereImage
转换为
javafx.scene.image.image
,您可以使用类似于

Image image = SwingFXUtils.toFXImage(imagem, null);
然后您可以应用
ColorAdjust

ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(0.1);
colorAdjust.setHue(-0.05);
colorAdjust.setBrightness(0.1);
colorAdjust.setSaturation(0.2);

ImageView imageView = new ImageView(image);
imageView.setFitWidth(image.getWidth());
imageView.setPreserveRatio(true);
imageView.setEffect(colorAdjust);
然后再把它转换回来

imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
这个想法是从我的电脑里偷来的。我不知道的是,如果需要首先在屏幕上实现
ImageView
,所有这些都不是

已更新

请注意,您正在跨越两个不同的UI框架,就像电影中所说的,“不要跨越流!”

JavaFX有一套比Swing更严格控制的需求,这既是好事也是坏事

您必须做的是让JavaFX代码在它的事件线程中运行。这比听起来更复杂(而且似乎需要),例如

原版|颜色调整(取自)|单色

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

public class Test extends Application {

    public static void main(String[] args) {
        Application.launch();
    }

    @Override
    public void start(Stage stage) throws Exception {

        try {
            System.out.println("Load image...");
            BufferedImage imagem = ImageIO.read(new File("..."));
            Image image = SwingFXUtils.toFXImage(imagem, null);

            ColorAdjust colorAdjust = new ColorAdjust();
            colorAdjust.setHue(0);
            colorAdjust.setSaturation(-1);
            colorAdjust.setBrightness(0);
            colorAdjust.setContrast(0);
//          colorAdjust.setHue(-0.05);
//          colorAdjust.setSaturation(0.2);
//          colorAdjust.setBrightness(0.1);
//          colorAdjust.setContrast(0.1);

            ImageView imageView = new ImageView(image);
            imageView.setFitWidth(image.getWidth());
            imageView.setPreserveRatio(true);
            imageView.setEffect(colorAdjust);

            System.out.println("Convert and save...");
            imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
            ImageIO.write(imagem, "png", new File("ColorAdjusted.png"));
        } catch (IOException exp) {
            exp.printStackTrace();
        } finally {
            Platform.exit();
        }
    }

}

下一件事是尝试解决如何使其作为实用程序类工作…

您需要将
buffereImage
转换为
javafx.scene.image.image
,您可以使用以下内容

Image image = SwingFXUtils.toFXImage(imagem, null);
然后您可以应用
ColorAdjust

ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(0.1);
colorAdjust.setHue(-0.05);
colorAdjust.setBrightness(0.1);
colorAdjust.setSaturation(0.2);

ImageView imageView = new ImageView(image);
imageView.setFitWidth(image.getWidth());
imageView.setPreserveRatio(true);
imageView.setEffect(colorAdjust);
然后再把它转换回来

imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
这个想法是从我的电脑里偷来的。我不知道的是,如果需要首先在屏幕上实现
ImageView
,所有这些都不是

已更新

请注意,您正在跨越两个不同的UI框架,就像电影中所说的,“不要跨越流!”

JavaFX有一套比Swing更严格控制的需求,这既是好事也是坏事

您必须做的是让JavaFX代码在它的事件线程中运行。这比听起来更复杂(而且似乎需要),例如

原版|颜色调整(取自)|单色

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

public class Test extends Application {

    public static void main(String[] args) {
        Application.launch();
    }

    @Override
    public void start(Stage stage) throws Exception {

        try {
            System.out.println("Load image...");
            BufferedImage imagem = ImageIO.read(new File("..."));
            Image image = SwingFXUtils.toFXImage(imagem, null);

            ColorAdjust colorAdjust = new ColorAdjust();
            colorAdjust.setHue(0);
            colorAdjust.setSaturation(-1);
            colorAdjust.setBrightness(0);
            colorAdjust.setContrast(0);
//          colorAdjust.setHue(-0.05);
//          colorAdjust.setSaturation(0.2);
//          colorAdjust.setBrightness(0.1);
//          colorAdjust.setContrast(0.1);

            ImageView imageView = new ImageView(image);
            imageView.setFitWidth(image.getWidth());
            imageView.setPreserveRatio(true);
            imageView.setEffect(colorAdjust);

            System.out.println("Convert and save...");
            imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
            ImageIO.write(imagem, "png", new File("ColorAdjusted.png"));
        } catch (IOException exp) {
            exp.printStackTrace();
        } finally {
            Platform.exit();
        }
    }

}

下一件事是尝试解决如何使其作为实用程序类工作…

您需要将
buffereImage
转换为
javafx.scene.image.image
,您可以使用以下内容

Image image = SwingFXUtils.toFXImage(imagem, null);
然后您可以应用
ColorAdjust

ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(0.1);
colorAdjust.setHue(-0.05);
colorAdjust.setBrightness(0.1);
colorAdjust.setSaturation(0.2);

ImageView imageView = new ImageView(image);
imageView.setFitWidth(image.getWidth());
imageView.setPreserveRatio(true);
imageView.setEffect(colorAdjust);
然后再把它转换回来

imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
这个想法是从我的电脑里偷来的。我不知道的是,如果需要首先在屏幕上实现
ImageView
,所有这些都不是

已更新

请注意,您正在跨越两个不同的UI框架,就像电影中所说的,“不要跨越流!”

JavaFX有一套比Swing更严格控制的需求,这既是好事也是坏事

您必须做的是让JavaFX代码在它的事件线程中运行。这比听起来更复杂(而且似乎需要),例如

原版|颜色调整(取自)|单色

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

public class Test extends Application {

    public static void main(String[] args) {
        Application.launch();
    }

    @Override
    public void start(Stage stage) throws Exception {

        try {
            System.out.println("Load image...");
            BufferedImage imagem = ImageIO.read(new File("..."));
            Image image = SwingFXUtils.toFXImage(imagem, null);

            ColorAdjust colorAdjust = new ColorAdjust();
            colorAdjust.setHue(0);
            colorAdjust.setSaturation(-1);
            colorAdjust.setBrightness(0);
            colorAdjust.setContrast(0);
//          colorAdjust.setHue(-0.05);
//          colorAdjust.setSaturation(0.2);
//          colorAdjust.setBrightness(0.1);
//          colorAdjust.setContrast(0.1);

            ImageView imageView = new ImageView(image);
            imageView.setFitWidth(image.getWidth());
            imageView.setPreserveRatio(true);
            imageView.setEffect(colorAdjust);

            System.out.println("Convert and save...");
            imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
            ImageIO.write(imagem, "png", new File("ColorAdjusted.png"));
        } catch (IOException exp) {
            exp.printStackTrace();
        } finally {
            Platform.exit();
        }
    }

}

下一件事是尝试解决如何使其作为实用程序类工作…

您需要将
buffereImage
转换为
javafx.scene.image.image
,您可以使用以下内容

Image image = SwingFXUtils.toFXImage(imagem, null);
然后您可以应用
ColorAdjust

ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(0.1);
colorAdjust.setHue(-0.05);
colorAdjust.setBrightness(0.1);
colorAdjust.setSaturation(0.2);

ImageView imageView = new ImageView(image);
imageView.setFitWidth(image.getWidth());
imageView.setPreserveRatio(true);
imageView.setEffect(colorAdjust);
然后再把它转换回来

imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
这个想法是从我的电脑里偷来的。我不知道的是,如果需要首先在屏幕上实现
ImageView
,所有这些都不是

已更新

请注意,您正在跨越两个不同的UI框架,就像电影中所说的,“不要跨越流!”

JavaFX有一套比Swing更严格控制的需求,这既是好事也是坏事

您必须做的是让JavaFX代码在它的事件线程中运行。这比听起来更复杂(而且似乎需要),例如

原版|颜色调整(取自)|单色

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

public class Test extends Application {

    public static void main(String[] args) {
        Application.launch();
    }

    @Override
    public void start(Stage stage) throws Exception {

        try {
            System.out.println("Load image...");
            BufferedImage imagem = ImageIO.read(new File("..."));
            Image image = SwingFXUtils.toFXImage(imagem, null);

            ColorAdjust colorAdjust = new ColorAdjust();
            colorAdjust.setHue(0);
            colorAdjust.setSaturation(-1);
            colorAdjust.setBrightness(0);
            colorAdjust.setContrast(0);
//          colorAdjust.setHue(-0.05);
//          colorAdjust.setSaturation(0.2);
//          colorAdjust.setBrightness(0.1);
//          colorAdjust.setContrast(0.1);

            ImageView imageView = new ImageView(image);
            imageView.setFitWidth(image.getWidth());
            imageView.setPreserveRatio(true);
            imageView.setEffect(colorAdjust);

            System.out.println("Convert and save...");
            imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
            ImageIO.write(imagem, "png", new File("ColorAdjusted.png"));
        } catch (IOException exp) {
            exp.printStackTrace();
        } finally {
            Platform.exit();
        }
    }

}

下一件事是尝试找出如何将其作为实用程序类使用…

示例解决方案

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

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

  • 将Swing/AWT转换为JavaFX
  • 使用JavaFX效果修改图像
  • 对颜色调整后的图像进行一次调整,以创建一个新的JavaFX图像
  • 新的JavaFX映像返回到新的Swing/AWT BuffereImage
  • 由于该解决方案混合了两种不同的工具包,因此在创建时应考虑以下因素:

  • 注意用于确保为给定工具箱调用使用正确类的导入;e、 例如,JavaFX和Swing/AWT都有
    Color
    Image
    类,因此有必要确保在正确的上下文中使用给定工具包的完全限定类-将Swing图像直接传递给javafxapi是错误的,反之亦然

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

  • 必须先初始化JavaFX工具包,然后才能使用它。通常,当应用程序