Can Picasso crop an image without scaling? - android

I have an image that I'm trying to load with Picasso that I want to crop without scaling.
Is this possible?
For example, if I had:
an image that's 400x800
an image view that's 200x400
Any questions I've seen regarding cropping suggest using resize(x, y).centreCrop(alignGravity) (i.e. scaling and cropping rather than only cropping).
In the example, you'd get the entire image scaled to 50%, as the JavaDocs for centreCrop mentions:
This cropping technique scales the image so that it fills the requested bounds and then crops the extra, preferring the contents at alignGravity.
I want to display the middle 200x400 pixels of the image and lose the edges entirely (similar to how ImageMagick's crop would work).
Is there a way to crop without scaling?

So it seems that out-of-the-box, no it doesn't, but you can use the Picasso Transformations library to do so:
implementation 'jp.wasabeef:picasso-transformations:2.4.0'
and then:
Picasso.get()
.load(largeImage)
.transform(new CropTransformation(width, height, GravityVertical.CENTER, GravityVertical.CENTER))
.into(imageView, /* callback */);
If you are using resize() or centreCrop() you need to use after the call to transform().

Related

Display images with proper dimensions before loading using glide and recyclerview

How do I display images in a facebook like feed? So far I've been using match_parent for width and wrap_content or fixed height. This works well but I need to show proper blank space for image feeds while it's getting lazy loaded. Like how do I set the dimensions properly for the image before loading it from the internet.
I think about getting the aspect ratio or the image dimensions itself via the api. Is it the only correct way or is there some other better way to achieve it using glide or any image loading library?
If you are using glide, you can set a place holder image or drawable with the dimensions you need while the real image is being downloaded.
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.placeholder(R.mipmap.ic_launcher) // can also be a drawable
.into(imageViewPlaceholder);

Picasso Library: Does Fit() function resize the image?

I am currently using the Picasso to load the image file to image view on android. I tried to have an image resized then using fit to fit on the imageView. The fit cannot be used with resize.
Picasso.With().Load().Resize().Fit().Into()
Does the fit function will resize the image.
From the Document of Picasso
Attempt to resize the image to fit exactly into the target ImageView's
bounds. This will result in delayed execution of the request until the
ImageView has been laid out.
So i think the fit function will resize the image.

Implementation for custom image cropping in Android

I need to implement the custom image cropping instead of using the system cropping (i.e. "com.android.camera.action.CROP"). I need to know the exact position of the cropping bounding box but this information is not retrievable if I choose to use the default cropping. Besides, the bitmap image is down sampled too much by default cropping.
The steps are as following:
Create the original size bitmap from source (using uri). The
original size is about 4000x3000 which is too big.
The user defines the crop area to extract the ROI which results in resizing of the original image to fit the ImageView. (size of ImageView is about 700x700)
Record the position of the bounding box in the ImageView.
Retrieve the cropped area from the original image and create another bitmap for it.
Resize the cropped bitmap to fit the imageview size to show it on the screen.
This approach works on my device (ZTE nuoio with Android 4.3) well. However, the app crashes on Samsung S4 with Android 4.4.4 and Note 4 while performing step 1 probably because of the out-of-memory error.
Therefore, I try to do another approach that creates the bitmap which is down sampled from the source image, rather than having the original size bitmap image.
I need to have the information of the exact position of cropped area from the original image. That is the reason why I didn't use default cropping. Could you please help me out with my case either providing
the solutions to derive the exact coordinates of the bounding box of the cropped image in the original image as a matrix.
how to solve the out-of-memory error in step 1 using the approach I mentioned above.
Or other approach to achieve image cropping with knowing the exact coordinates of cropped area form the original image.
Thank you so much.
For Crop an image and get Coordinates use library Edmodo Croper https://github.com/edmodo/cropper
For Out of memory issue you have to down scale image.

How "customizable" is Picasso vs. Universal Image Loader (UIL)?

My goal is to use one large image containing a textured background for my Android app. At run-time I want cut out a screen-size piece of it and place it as the background image.
Which Image Loader would be better for this?
This review of Android Image Loader libraries described some procs and cons of the Picasso library and the UIL library (among others).
So far I think Picasso seems to match my needs. "Picasso allows you to specify
exact target image size." I believe this would accomplish my goal of cropping out a specific size of image, based on the device's screen size and density.
This article claims that UIL allows for a lot of customization, but then it also says it "doesn’t not provide a way to specify image size directly you want to load into a view".
Am I correct that Picasso will better allow me to crop an image to the size of the actual screen size?
By default, if I remember correctly, Picasso isn't able to crop an image out of the box. But, Picasso allows you to code transformations, and specify their use, as part of the loading process.
Picasso.with(ctx).load(uri)
.transform(new YourCropTransform(params...))
.into(target)

Picasso - transformation bitmap quality

I am downloading low resolution pictures from web by using Picasso and displaying it in high res displays. Fetching images is straightforward:
Picasso.with(context)
.load(item.getPicture())
.transform(new BitmapTransformations.OverlayTransformation(
context.getResources(), R.drawable.ic_play_video))
.error(R.drawable.picture_placeholder)
.into(target);
Notice that I apply Overlay transformation into request - It's a high quality image asset I put on top of downloaded image. Applying overlay is also simple - just resize bitmap and put it in the center of image. But here comes the problem: Unless I add resizing, in my case:
.fit().centerCrop();
into Picasso request, overlaid high-res asset is copying quality of downloaded image (first picture). When I add this resizing before transformation the overlay looks as expected (second picture). Can anyone explain to me why this happens? Thanks you!

Categories

Resources