【问题标题】:Partial blur effect on image, javafx对图像的部分模糊效果,javafx
【发布时间】:2018-03-10 15:54:29
【问题描述】:

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

something like this

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

【问题讨论】:

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


    【解决方案1】:

    如果我正确理解您的问题,您需要一个在特定部分模糊的移动 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;
    }
    

    2。使用库

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

    【讨论】:

      【解决方案2】:

      考虑将图像的两个副本放入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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-21
        • 2017-12-21
        • 1970-01-01
        • 1970-01-01
        • 2015-07-13
        • 2012-09-28
        相关资源
        最近更新 更多