如何使用Picasso模糊图像?

6
我想使用Picasso下载图片后实现模糊效果。我找到了一个库,可以为我的图片添加模糊效果:https://github.com/wasabeef/picasso-transformations 这是我使用的代码:
 Picasso.get()
            .load(currentEvent.posterDownloadPath)
            .transform(BlurTransformation(25,3))
            .into(recommendedEventViewHolder.blurryImageView)

但不幸的是我遇到了错误:

enter image description here

我尝试了几种方法,但仍然无法正确地使用这个库的模糊效果。请问您能否帮我添加模糊效果?也许您有不同的方法(不使用这个库)?

Java没问题。


我转用了Glide库,因为我在使用Picasso时也遇到了一些问题。https://futurestud.io/tutorials/glide-custom-transformation - Vince VD
Glide库 https://github.com/bumptech/glide - Vince VD
@Vince 是的,我最初使用Glide,然后因为这个问题 https://stackoverflow.com/questions/55084585/why-my-glide-doesnt-cache-the-image-it-seems-download-image-all-over-again 而改用Picasso。 - Alexa289
2个回答

12

毕加索

使用毕加索,请按照以下步骤:

步骤1

在 Gradle 中添加这些依赖项:

repositories {
    jcenter()
}

dependencies {
    compile 'jp.wasabeef:picasso-transformations:2.2.1'
    // If you want to use the GPU Filters
    compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1'
}

步骤 2

设置 Picasso 转换:

Picasso.get()
       .load(currentEvent.posterDownloadPath)
       .transform(new BlurTransformation(mContext, 25, 1))
       .into(recommendedEventViewHolder.blurryImageView);

在 Kotlin 中像这样:

Picasso.get()
           .load(currentEvent.posterDownloadPath)
           .transform(BlurTransformation(mContext, 25, 1))
           .into(recommendedEventViewHolder.blurryImageView)

此外,您还可以使用Blurry。Blurry是一个易于使用的Android模糊库。但我建议使用Fresco。Fresco是Android应用程序中显示图像的强大系统。

Fresco

要使用Fresco,请按照以下步骤进行:

步骤1:创建Android项目,并将fresco库依赖添加到模块的build.gradle文件中并同步项目。

dependencies {
implementation 'com.facebook.fresco:fresco:1.13.0'
implementation 'jp.wasabeef:fresco-processors:2.1.0'
}

步骤2:在MainActivity.java或Application.java的onCreate()方法中初始化Fresco

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //initialize fresco
    Fresco.initialize(this);
    setContentView(R.layout.activity_main);

}

第三步: 在你的activity_main.xml文件中添加SimpleDraweeView, 它是由Fresco库提供的。

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/sdv_image"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:src="@mipmap/ic_launcher" />

步骤4:在编写图像模糊代码之前,您需要处理三个重要的类。

i. 后处理器: -定义图像模糊质量。

ii. ImageRequest: -使用Postprocessor实例创建控制器请求。

iii. PipelineDraweeController: -使用ImageRequest和SimpleDraweeView实例为视图准备控制器。

步骤5:在主活动类中创建一个BlurPostprocessor实例,并传入上下文和半径参数,其中半径指定图像的模糊百分比。

Postprocessor postprocessor = new BlurPostprocessor(this,50);
Step6:使用Postprocessor实例中保存的模糊属性构建图像请求的ImageRequest类。
 ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse("image url"))
        .setPostprocessor(postprocessor)
        .build();

步骤7:使用ImageRequest和旧的SimpleDraweeView创建PipelineDraweeController的新实例。

controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
        .setImageRequest(imageRequest)
        .setOldController(simpleDraweeView.getController())
        .build();

步骤8: 将控制器传递给simpleDraweeView。

simpleDraweeView.setController(controller);

就这些了,现在构建并运行应用程序。

享受吧 :)


我已经尝试过了,但是我遇到了另一个错误,就像这个 https://i.stack.imgur.com/r9dSm.png,我正在使用 Kotlin。是的,我看过那个模糊库。但不幸的是,我有点困惑如何使用该库。我有一个图像路径,我想将下载的图像放在图像视图中,但我不知道如何使用 Blurry 实现,你能否添加代码来解决这个问题? - Alexa289
@Alexa289 你必须使用 .transform(new BlurTransformation(mContext, 25, 1))。 - Masoud Mokhtari
@Alexa289 你确定吗?我尝试了这个,它正确地工作了。你用什么语言编码?Android还是Kotlin? - Masoud Mokhtari
是的,100%。我正在使用 Kotlin,Kotlin 中没有 'new'。 - Alexa289
@Alexa289,我在Kotlin中测试了这段代码,它是正确的。你必须使用.transform(BlurTransformation(yourContext, 25, 1)) - Masoud Mokhtari
显示剩余4条评论

1
这段代码对我有用:两个变换都来自wasabeef库。
 SketchFilterTransformation transformation  = new SketchFilterTransformation(this);

 BlurTransformation transformation1 = new BlurTransformation(this);

 Picasso.get()
        .load(mImageUri)
        .transform(transformation)
        .transform(transformation1)
        .rotate(decodeRotation(orientation))
        .into((ImageView) findViewById(R.id.image));

URI是文件选择器的数据。

我在我的Build gradle中有这个:

 implementation 'com.squareup.picasso:picasso:2.71828'
 implementation 'jp.wasabeef:picasso-transformations:2.2.1'
 // If you want to use the GPU Filters
 implementation 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接