合并图像以进行叠加

时间:2015-04-17 09:46:46

标签: image javafx

我尝试做类似这篇文章的内容:related question

现在我尝试组合几个模板。组合模板看起来如预期,但是反转组是黑色或白色,具体取决于创建的组的顺序。结果我得到: Result

似乎分组仍然需要孩子的一些混合信息,或者我可能只是不了解混合物的想法。任何想法我如何能够在旧问题中实现覆盖,但有几个模板?

以下是图片:
original stencil1 stencil2 stencil3

package application;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BlendMode;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {
@Override
public void start(Stage stage) {
    Image original = new Image(getClass().getResourceAsStream("image.jpg"));

    Image stencil1 = new Image(getClass().getResourceAsStream("stencil.jpg"));

    Image stencil2 = new Image(getClass().getResourceAsStream("stencil2.jpg"));

    Image stencil3 = new Image(getClass().getResourceAsStream("stencil3.jpg"));

    ImageView iv = new ImageView(stencil1);
    ImageView iv2 = new ImageView(stencil2);
    ImageView iv3 = new ImageView(stencil3);

    iv2.setBlendMode(BlendMode.ADD);
    iv3.setBlendMode(BlendMode.ADD);
    Group stencil = new Group();

    stencil.getChildren().add(iv);
    stencil.getChildren().add(iv2);
    stencil.getChildren().add(iv3);

    Rectangle whiteRect = new Rectangle(original.getWidth(), original.getHeight());
    whiteRect.setFill(Color.WHITE);
    whiteRect.setBlendMode(BlendMode.DIFFERENCE);

    Group inverted = new Group(stencil, whiteRect);

    // display the original, composite image and stencil.
    HBox layout = new HBox(10);
    layout.getChildren().addAll(new ImageView(original), inverted, stencil);
    layout.setPadding(new Insets(10));
    stage.setScene(new Scene(layout));
    stage.show();

}

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

1 个答案:

答案 0 :(得分:0)

我终于弄明白了。诀窍是始终创建ImageView对象的新实例,因为混合模式沿着场景图形应用到图像视图对象。
这是剪辑的代码对我有用:         包裹申请;

    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.effect.BlendMode;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.HBox;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;

    public class Main extends Application {

        private ImageView originalImageView;
        private ImageView stencilImageView;
        private Image original;
        private Image stencil;
        private Image stencil2;

        private Rectangle whiteRect;
        private Group inverted;

        private Group overlaidBlack;

        private Rectangle redRect;

        private Group redStencil;
        private Group overlaidRed;

        private ImageView stencilImageView2;
        private Group to;
        private Group to2;


        @Override
        public void start(Stage stage) {

            original = new Image(getClass().getResourceAsStream("image.jpg"));

            stencil = new Image(getClass().getResourceAsStream("stencil.jpg"));
            stencil2 = new Image(getClass().getResourceAsStream("stencil2.jpg"));

            stencilImageView = new ImageView(stencil);

            // first invert the stencil so that it is black on white rather than white on black.
            whiteRect = new Rectangle(stencil.getWidth(), stencil.getHeight());
            whiteRect.setFill(Color.WHITE);
            whiteRect.setBlendMode(BlendMode.DIFFERENCE);

            stencilImageView2 = new ImageView(stencil2);

            stencilImageView2.setBlendMode(BlendMode.ADD);

            to = new Group(stencilImageView, stencilImageView2);

            ImageView tmpIv = new ImageView(stencil2);
            tmpIv.setBlendMode(BlendMode.ADD);
            to2 = new Group(new ImageView(stencil), tmpIv);

            inverted = new Group(to, whiteRect);

            originalImageView = new ImageView(original);

            overlaidBlack = new Group(originalImageView, inverted);

            inverted.setBlendMode(BlendMode.MULTIPLY);

            overlaidBlack = new Group(originalImageView, inverted);

            // create a new mask with a red tint (red on black).
            redRect = new Rectangle(stencil.getWidth(), stencil.getHeight());
            redRect.setFill(Color.RED);
            redRect.setBlendMode(BlendMode.MULTIPLY);

            redStencil = new Group(to2, redRect);

            redStencil.setBlendMode(BlendMode.ADD);

            overlaidRed = new Group(overlaidBlack, redStencil);

            // display the original, composite image and stencil.
            HBox layout = new HBox(10);
            layout.getChildren().addAll(new ImageView(original), overlaidRed);

            layout.setPadding(new Insets(10));
            stage.setScene(new Scene(layout));
            stage.show();
        }

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