Blur thumbnail image before loading actual image in picasso - android

i am using picasso to display the image from URL, i am displaying thumbnail image first before loading actual image, i want to blur that thumbnail image , how i can achieve in picasso ?
here is my source code
pb.setVisibility(View.GONE);
Picasso.with(getApplicationContext())
.load(thumbUrl) // thumbnail url goes here
//.placeholder(R.drawable.progress_animation)
.resize(width, width)
.transform(new BlurTransformation(getApplicationContext(), 25, 1))
.into(imageview, new Callback()
{
#Override
public void onSuccess()
{
pb.setVisibility(View.GONE);
Log.e(TAG,"OnSuccess");
Picasso.with(getApplicationContext())
.load(url) // image url goes here
.resize(width, width)
.placeholder(imageview.getDrawable())
.into(imageview);
iv_reDownload.setVisibility(View.GONE);
}
#Override
public void onError()
{
pb.setVisibility(View.GONE);
Log.e(TAG,"OnError");
Picasso.with(getApplicationContext())
.load(url) // image url goes here
.resize(width, width)
.placeholder(imageview.getDrawable())
.into(imageview);
iv_reDownload.setVisibility(View.VISIBLE);
}
});

To my knowledge Picasso currently doesn't support this "feature". Glide library, which has very similar usage, does have a function that enables easy usage of "fast preview", that is low resolution thumbnail which is loaded faster than full size image.
Function is thumbnail(float) and accepts scale factor as argument, where for example 0.1f is one-tenth of original image size.
Case usage with Glide could be something like this.
Glide.with(mFragment)
.load(wFile.getUriForThumb())
.override(length, length)
.thumbnail(.05f)
.placeholder(R.drawable.placeholder_image_128px)
.into(holder.mImageThumb);
Where method override is an close equivalent to resize(int,int) in Picasso.
For complete comparison between two libraries you can check this nice guide: Glide vs. Picasso

Blur Transformation in Picasso, Reference: https://futurestud.io/tutorials/picasso-image-rotation-and-transformation
#Override
public Bitmap transform(Bitmap bitmap) {
// Create another bitmap that will hold the results of the filter.
Bitmap blurredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
// Allocate memory for Renderscript to work with
Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
Allocation output = Allocation.createTyped(rs, input.getType());
// Load up an instance of the specific script that we want to use.
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setInput(input);
// Set the blur radius
script.setRadius(10);
// Start the ScriptIntrinisicBlur
script.forEach(output);
// Copy the output to the blurred bitmap
output.copyTo(blurredBitmap);
bitmap.recycle();
return blurredBitmap;
}

finally, i solved my problem with the help of this Library named as picasso-transformations , i added the below dependencies in my project
compile 'jp.wasabeef:picasso-transformations:2.2.1'
not only it support Picasso but also it support Glide or Fresco too
and i called the BlurTransformation class inside picasso.
here is complete example
pb.setVisibility(View.GONE);
Picasso.with(getApplicationContext())
.load(thumbUrl) // thumbnail url goes here
//.placeholder(R.drawable.progress_animation)
.resize(width, width)
.transform(new BlurTransformation(getApplicationContext(), 25, 1))
.into(imageview, new Callback()
{
#Override
public void onSuccess()
{
pb.setVisibility(View.GONE);
Log.e(TAG,"OnSuccess");
Picasso.with(getApplicationContext())
.load(url) // image url goes here
.resize(width, width)
.placeholder(imageview.getDrawable())
.into(imageview);
iv_reDownload.setVisibility(View.GONE);
}
#Override
public void onError()
{
pb.setVisibility(View.GONE);
Log.e(TAG,"OnError");
Picasso.with(getApplicationContext())
.load(url) // image url goes here
.resize(width, width)
.placeholder(imageview.getDrawable())
.into(imageview);
iv_reDownload.setVisibility(View.VISIBLE);
}
});

I have tested both Glide and Picasso for fast loading if you resize your image in Picasso the it will improve your loading speed, here is the code
Glide.with(context)
.load(sArr.get(position).getThumbnail())
.thumbnail(0.01f)
.override(100,100)
.centerCrop()
.placeholder(R.drawable.default_picture)
.into(holder.imgItem);
Code for Piccaso
Picasso.with(context)
.load(sArr.get(position).getThumbnail())
.resize(100,100)
.placeholder(R.drawable.default_picture)
.into(holder.imgItem);
and Picasso win !!

Not sure if this will work but I have seen before that you can use:
.add(new ImageTransform(IMAGE_URL, new BlurTransformation(this)));

Related

Android load pictures from firestorage with Glide

I load user's picture from firestorage with this code but it has some issues like the pics blink when scroll, reload every time I open the app and sometimes the pics take a long time to load I try to add .diskCacheStrategy(DiskCacheStrategy.NONE)
because it takes too long to change the picture, I need help to load it faster and make it not to blink
StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images/"+ theUID);
Glide.with(context.getApplicationContext())
.using(new FirebaseImageLoader())
.load(storageReference)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(new BitmapImageViewTarget(pic) {
#Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(),
Bitmap.createScaledBitmap(resource, 150, 150, false));
drawable.setCircular(true);
pic.setImageDrawable(drawable);
}
});

Get bitmap from imageview in viewpager with glide

I'm showing in imageview with glide. User can save image on button click.
Bitmap bitmap = ((BitmapDrawable)imageViewPreview.getDrawable()).getBitmap();
when i use this code it save whole screen image(Image and black space).
imageViewPreview.setDrawingCacheEnabled(true);
imageViewPreview.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
imageViewPreview.layout(0, 0,
imageViewPreview.getMeasuredWidth(), imageViewPreview.getMeasuredHeight());
imageViewPreview.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(imageViewPreview.getDrawingCache());
imageViewPreview.setDrawingCacheEnabled(false);
when is use this, sometime it work but image get cropped in half or image go to up like "android:layout_alignParentTop="true".
You can get Bitmap from DrawingCache. After Settting image in ImageView you can get it as below:
imageViewPreview.setDrawingCacheEnabled(true);
imageViewPreview.buildDrawingCache(true);
final Bitmap bmp = Bitmap.createBitmap(layout.getDrawingCache());
imageViewPreview.setDrawingCacheEnabled(false);
If you need full size image then this is not right way i think .A better solution would be to get it From Glide as Bitmap . If full image was loaded already it will be quick (from cache) if not then it will load the full image from server or you can still use Drawing cache in case of No internet connection and glide failure.
Glide.with(imageView.getContext())
.asBitmap()
.load("imageUrl")
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
// Use resource here
}
});
}

How to get bitmap from imageView

I need to take an image from imageView and make it as a bitmap.
String s = getIntent().getStringExtra("ImageUri");
mImageViewFilter = (ImageView) findViewById(R.id.imageViewFilter);
Glide.with(this)
.load(s)
.into(mImageViewFilter);
Bitmap bitmap = ((BitmapDrawable)mImageViewFilter.getDrawable()).getBitmap();
You can see that into mImageViewFilter I am loading a photo with use of its URI and Glide library.
My next step is to take this photo which is in mImageViewFilter and make it as a bitmap. After the last line of code there is an exception. What am I doing wrong?
String s = getIntent().getStringExtra("ImageUri");
mImageViewFilter = (ImageView) findViewById(R.id.imageViewFilter);
To load image into ImageView use below code
Glide.with(this).load(s).into(mImageViewFilter);
So in order to get bitmap from ImageView (mImageViewFilter )
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
OR
You can add this before the code where you are trying to get Bitmap out of the ImageView
mImageViewFilter.setDrawingCacheEnabled(true);
In order to get the Bitmap out of the ImageView using DrawingCache, you first need to enable ImageView to draw image cache.
Bitmap bitmap = mImageViewFilter.getDrawingCache();
Actually you doing right thing. But the problem is Image loading is Asynchronous call.
And you called getBitmap() in right next line which will be called sequentially.
And it will give you null anyway cause image is not loaded yet .
Solution
if you want to do it in next direct step .You can use direct Bitmap callback.
Glide.with(this)
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
// Use resource as bitmap save it for later.
}
});
First take it as bitmap, then take it again to apply to our ImageView :
Bitmap bitmap = Glide.with(this) //taking as bitmap
.load(s)
.asBitmap()
.into(100, 100) //width and height
.get();
Glide.with(this) //apply to imageview
.load(s)
.into(mImageViewFilter);
The straightforward way to obtain a bitmap from any View including ImageView is to get the drawing cache from it.
mImageViewFilter.buildDrawingCache();
Bitmap bitmap = mImageViewFilter.getDrawingCache();
But the above method may give you a low quality image.
I think you should directly decode the bitmap from the Uri using BitmapFactory
InputStream in = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(in);
in.close();

How to get image width and height from a network url

I am working on an android app and using REST APIs in it. I am receiving image urls from server and I want to show these images according to the original height of the image using image url.
How can I get the width and height of image using network image url. I have already tried getting bitmap from the string url but for this I want to perform this in background thread which leads the image loading very slow.
Is there any way I can get the image width and height from image url and can show the image in imageview according to the width and height.
Please help me if anyone know about this.
Thanks a lot in advanced.
URL url = new URL("url/image.jpg");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
for width:
bmp.getWidth();
for height:
bmp.getHeight();
for setting in imageview:
ImageView.setImageBitmap(bmp);
Further Reference:
https://developer.android.com/reference/android/graphics/Bitmap.html#getHeight%28%29
/* your library */
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
/* your code */
Glide.with(this)
.asBitmap() //get hieght and width
.load("link image")
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable
Transition<? super Bitmap> transition) {
Log.d("TAG", "onResourceReady: "+resource.getWidth()+"
"+resource.getHeight());
}
});
//////////////////////////////////////
Glide.with(this)
.asFile() // get size Image
.load("link image")
.into(new SimpleTarget<File>() {
#Override
public void onResourceReady(#NonNull File resource, #Nullable
Transition<? super File> transition) {
fab_full.setLabelText(""+resource.length());
}
});

Crop Imageview using input Specific Length and width

I have app. I want to crop my picture that shown in imageview. I want crop with spesific length and width. can you help me? I have code but, that code crop direct from gallery.
I just want crop imageview with spesific length and width. Where length and width just input manual from user.
If you don't mind adding a new Library to your project just to do image loading for you, you can easily add Glide to crop your local drawables or images from the web.
In your build.gradle (module app)
compile 'com.github.bumptech.glide:glide:3.6.0'
You can crop the image with this below:
final ImageView myImage = (ImageView) findViewById(R.id.myimageview);
final int myWidth = 210;
final int myHeight = 80;
Glide.with(getApplicationContext())
.load(R.drawable.example_drawable) // you can add any drawable or url of an image here
.asBitmap()
.into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
// Do something with bitmap here.
Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, myWidth, myHeight);
myImage.setImageBitmap(bm);
}
});

Categories

Resources