对图像的部分模糊效果,javafx

时间:2018-03-10 15:54:29

标签: javafx imageview javafx-8 blur motion-blur

我有一个特定的问题,我试图找到整个互联网,但没有运气。用户将选择一个人的模型(图像视图),并通过移动其位置,模糊效果将出现从男人那里下来/从右到左。基本上,我需要在图像视图上使用模糊效果,但可以选择设置我想要模糊的图像部分。

something like this

https://www.freepik.com/free-photo/young-man-walking_1214201.htm'>由Freepik设计

2 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,您需要一个在特定部分模糊的移动ImageView。为此,您可以使用onTouch事件将ImageView设置为可移动。 为了模糊图像,您可以使用一些hacky技术或使用Renderscript非常有效地模糊图像。 Mario Viviani描述了here

模糊图片的代码:

public Bitmap blurBitmap(Bitmap bitmap) {

//Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(getApplicationContext());

//Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

//Create the in/out Allocations with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

//Set the radius of the blur
blurScript.setRadius(25.f);

//Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);

//Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);

//recycle the original bitmap
bitmap.recycle();

//After finishing everything, we destroy the Renderscript.
rs.destroy();

return outBitmap;
}

Output Image

<强> 2。使用资料库

你可以使用Dali Library在内部完成所有繁重的RenderScript并轻松实现模糊。

enter image description here

答案 1 :(得分:0)

考虑将图像的两个副本放入StackPane,将模糊应用于其中一个。然后只需将剪辑应用于每个图像,这样就可以看到一个图像的一半和另一个图像的一半:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class PartiallyBlurredImage extends Application {

    private final Image image = new Image("https://www.nasa.gov/sites/default/files/thumbnails/image/pia22226.jpg");
    private final double width = 600 ;
    private final double height = 800 ;


    @Override
    public void start(Stage primaryStage) {
        ImageView plainImage = createImageView();

        ImageView blurredImage = createImageView();
        GaussianBlur blur = new GaussianBlur(20);       
        blurredImage.setEffect(blur);


        plainImage.setClip(new Rectangle(0, 0, width/2, height));
        blurredImage.setClip(new Rectangle(width/2, 0, width/2, height));

        StackPane partiallyBlurredImage = new StackPane(plainImage, blurredImage);
        Scene scene = new Scene(new ScrollPane(partiallyBlurredImage));
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private ImageView createImageView() {
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(width);
        imageView.setFitHeight(height);
        imageView.setPreserveRatio(true);
        return imageView ;
    }

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

enter image description here