【JAVAFX 如何实现图片任意角度渐变处理】
-
简述
功能描述:图片处理PS就够了,但是在javafx中想对图片做一个动态处理,或者说滤镜特效来做资源组件,网上找了很多,基本没有,要么类似但不能用,需要图片产生渐变效果,任何角度位置生成渐变效果,并且透明,PS不说了非常简单,找了几天没有,虽然java 滤镜处理有 Java Image Filters, 或者网上有很多图片处理功能,非常好,但找起来也非常的头疼, 同时也看了<<用java实现图片渐变叠加(3种方法)>>例子非常好,但不是想要的,翻遍几天基本无果,只好自己实现效果. -
原图
-
处理效果
-
功能实现
import com.liangchao.mspm.utils.RS;
import com.liangchao.mspm.utils.javafx.ImageMorphing;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* Java 图片渐变
*
* @author Administrator
* @version 1.0
* @class galacticeyes--LCH
* @description:
* @date 2022/4/5 15:17
*/
public class ImageMorphingTest extends Application {
public static void main(String[] args) throws IOException {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
String path = "c:\\logs\\3.jpg";
ImageView imageView = new ImageView(RS.file(path));
imageView.setFitWidth(1100);
imageView.setSmooth(true);
imageView.setPreserveRatio(true);
HBox hbox = new HBox(5);
hbox.setAlignment(Pos.CENTER_LEFT);
// 颜色选择
ColorPicker colorPicker = new ColorPicker();
ColorPicker endColorPicker = new ColorPicker();
Slider xSlider = new Slider(0, imageView.getImage().getWidth(), 0);
Slider ySlider = new Slider(0, imageView.getImage().getHeight(), 0);
Slider xEndSlider = new Slider(0, imageView.getImage().getWidth(), imageView.getImage().getWidth());
Slider yEndSlider = new Slider(0, imageView.getImage().getHeight(), imageView.getImage().getHeight());
Button freshen = new Button("刷新");
freshen.setOnAction(event -> {
Color color = new Color(
(float) colorPicker.getValue().getRed(),
(float) colorPicker.getValue().getGreen(),
(float) colorPicker.getValue().getBlue(),
(float) colorPicker.getValue().getOpacity()
);
Color endColor = new Color(
(float) endColorPicker.getValue().getRed(),
(float) endColorPicker.getValue().getGreen(),
(float) endColorPicker.getValue().getBlue(),
(float) endColorPicker.getValue().getOpacity()
);
ImageMorphing imageMorphing = new ImageMorphing();
BufferedImage bufferedImage = imageMorphing.imageMorphing(
SwingFXUtils.fromFXImage(new Image(RS.file(path)), null),
color,
endColor,
(float) xSlider.getValue(),
(float) ySlider.getValue(),
(float) xEndSlider.getValue(),
(float) yEndSlider.getValue()
);
WritableImage fxImage = SwingFXUtils.toFXImage(bufferedImage, null);
imageView.setImage(fxImage);
try {
ImageIO.write(bufferedImage, "PNG", new File("c:\\logs\\3.png"));
} catch (IOException e) {
e.printStackTrace();
}
});
hbox.getChildren().addAll(
new Label("开始颜色"),
colorPicker,
new Label("结束颜色"),
endColorPicker,
new Label("X开始"),
xSlider,
new Label("Y开始"),
ySlider,
new Label("X结束"),
xEndSlider,
new Label("Y结束"),
yEndSlider,
freshen
);
//设置面板对象
VBox pane = new VBox(hbox, imageView);
Scene scene = new Scene(pane, 1100, 600);
primaryStage.setTitle("拖到操作");
primaryStage.setScene(scene);
primaryStage.show();
}
}
- 思路