JavaFX 图片颜色变换

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.*;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ImageGrayProcessed extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        FlowPane flowPane = new FlowPane();
        flowPane.getChildren().add(getImageView(0));
        flowPane.getChildren().add(getImageView(1));
        flowPane.getChildren().add(getImageView(2));
        flowPane.getChildren().add(getImageView(3));
        flowPane.getChildren().add(getImageView(4));
        flowPane.getChildren().add(getImageView(5));
        flowPane.getChildren().add(getImageView(6));
        flowPane.getChildren().add(getImageView(7));
        flowPane.setHgap(20);
        flowPane.setOrientation(Orientation.HORIZONTAL);
        flowPane.setAlignment(Pos.CENTER);
        flowPane.setPrefWidth(600);
        flowPane.setPrefHeight(60);
        primaryStage.setScene(new Scene(flowPane));
        primaryStage.show();
    }

    private ImageView getImageView(int type){
        Image image = new Image("http://tile.openstreetmap.org/1/1/0.png");
        System.out.println("type:" + type);
        if(type > 0){
            image = pixWithImage(type, image);
        }

        ImageView imageView = new ImageView(image);
        double imageFitHeight = 50;
        imageView.setFitHeight(imageFitHeight);
        imageView.setFitWidth(image.getWidth() / (image.getHeight() / imageFitHeight));

        return imageView;
    }

    /**
     * 根据传入类型变化图片像素
     */
    public Image pixWithImage(int type,Image image){
        PixelReader pixelReader = image.getPixelReader();
        if(image.getWidth()>0 && image.getHeight() >0){
            WritableImage wImage;
            wImage = new WritableImage(
                    (int)image.getWidth(),
                    (int)image.getHeight());
            PixelWriter pixelWriter = wImage.getPixelWriter();

            for(int y = 0; y < image.getHeight(); y++){
                for(int x = 0; x < image.getWidth(); x++){
                    Color color = pixelReader.getColor(x, y);
                    switch (type) {
                        case 1:
                            // 颜色变轻
                            color = color.brighter();
                            break;
                        case 2:
                            // 颜色变深
                            color = color.darker();
                            break;
                        case 3:
                            // 灰度化
                            color = color.grayscale();
                            break;
                        case 4:
                            // 颜色反转
                            color = color.invert();
                            break;
                        case 5:
                            // 颜色饱和
                            color = color.saturate();
                            break;
                        case 6:
                            // 颜色不饱和
                            color = color.desaturate();
                            break;
                        case 7:
                            // 颜色灰度化后反转(字黑体,背景鲜亮,可用于强字弱景)
                            color = color.grayscale();
                            color = color.invert();
                            break;
                        default:
                            break;
                    }

                    pixelWriter.setColor(x, y, color);
                }
            }
            return wImage;
        }
        return null;
    }
}

 

你可能感兴趣的:(Java,GIS,#,JavaFX,#,图像)