Poor Image Quality on scaling image in android - android

I am working on android application development...when i click on thumbnail image, it enlarges but give extreme poor quality...please help me how to resolve it
ImageView ivPreview = (ImageView)nagDialog.findViewById(R.id.image1);
int h =200 ; // height in pixels
int w = 200; // width in pixels
Bitmap Scaled = Bitmap.createScaledBitmap(bm, h, w, true);
ivPreview.setImageBitmap(Scaled);

If I understand correctly you want to enlarge a thumbnail. This is a bad idea because a thumbnails' size is a small image and enhancing it only makes it look worse. The Bitmap.createScaledBitmap() method stretches the bitmap resulting in quality loss. You should use a bigger original image, create the thubmnail from that image and load the original image when you click on it. Here's some code to get you going:
Bitmap original = //get original image
Bitmap thumbnail = Bitmap.createScaledBitmap(original, 50, 50, true);
//when the user clicks on the imageview:
imgView.setImageBitmap(original);

Related

Crop bottom portion of a bitmap programmatically

I have a bitmap taken from a camera. I want to crop the image so it only leaves the bottom portion of it. The cropped image should be 80% less the height of the original bitmap, so I want only the 20% of the bottom part starting from the left edge.
I'm doing this explicitly in the code without any Android cropping intent whatsoever.
An image to visualize what I want to achieve:
I've managed to crop the top part of the bitmap by using this code:
final Bitmap toBeCropped = BitmapFactory.decodeFile(mFile.getPath());
final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inTargetDensity = 1;
toBeCropped.setDensity(Bitmap.DENSITY_NONE);
int fromHere = (int) (toBeCropped.getHeight() * 0.2);
Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, 0, toBeCropped.getWidth(), fromHere);
mPreviewHalf.setImageBitmap(croppedBitmap);
But I couldn't find a way to start the cropping 80% from the top. I'm thinking of getting the y-coordinate of the Bitmap, so that I could crop any image sizes and always get the bottom portion only. But can anyone point to me how do I get this coordinate from a bitmap? Or do I have to take it from the layout itself?
I am not familiar with operations on Bitmaps but from inspecting your code and looking at the API my guess would be that you need to specify the y coordinates on the following line to match the starting point:
Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, "here", toBeCropped.getWidth(), fromHere);
So my guess would be something like the following:
Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, (toBeCropped.getHeight() * 0.8), toBeCropped.getWidth(), fromHere);
in this case fromHere will define the number of rows you want to crop not the starting point (which is 20% of the total as you have pointed out)
This is how I do it:
topcutoff is what you want to cut of on top of the image and buttomcutoff on the buttom (if needed)
height = height - topcutoff;
height = height - bottomcutoff;
croppedBitmap = Bitmap.createBitmap(croppedBitmap, 0, topcutoff, width, height);
Basically you just set a startpoint (topcutoff) from where to begin displaying the bitmap. In your case this would be the position after 80% of your bitmap.
This might also explain some things: Google Bitmap Documentation
"int: The y coordinate of the first pixel in source", so where you want to begin displaying your image.

How to properly set a wallpaper on Android

It seems that setting a wallpaper on Android just doesn't work in any useful way.
If you get an image from your phone and set it as the wallpaper, it's way too big for the screen
If you resize it (either using a createBitmap() function that allows you to specify size, or the ridiculously useless createScaledBitmap()) it goes all jaggy and out of focus
If you use some freaky hacks to fix the quality of the image, it's better, but still not perfectly clear by any stretch.
If you attempt to get the current wallpaper and set that, it still seems to make it too big. It appears to give you the original image file, forcing you to resize it, which doesn't work.
Now, the phone's internal software is perfectly capable of resizing an image to be smaller with no reduction of quality. Why does it not share this functionality?
WallpaperManager wallpaperManager = WallpaperManager.getInstance(Spinnerz.this);
// gets the image from file, inexplicably upside down.
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "DCIM/Camera/Zedz.jpg");
// use some rotation to get it on an angle that matches the screen.
Matrix bitmapTransforms = new Matrix();
bitmapTransforms.setRotate(90); // flip back upright
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),bitmapTransforms, false); // create bitmap from bitmap from file, using the rotate matrix
// lets set it exactly to the resolution of my Samsung Galaxy S2: 480x800 pixels.
// This function appears to exist specifically to scale a bitmap object - should do a good job of it!
bitmap = Bitmap.createScaledBitmap(bitmap, 480, 800, false);
wallpaperManager.setBitmap(bitmap);
// result is quite a jaggy image - not suitable as a wallpaper.
PIC:
Try this ,
Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();

Android - How to enlarge a bitmap

I usually download images that are larger in size and shrink them using an imageview but lately Im trying to deal with my apps not working on a lesser network connection so I was wondering how can I increase the size of an image once it gets to the device. I tried resizing the image once it was in an imageview but the imageview will get no larger than the original image. Im sure theres a really easy way to increase or blow up an image on the device but I havent come across it yet.
So.....how can I increase the size of an image. Id like to blow it up and use it in an imageview but the images Im dealing with are only 128X256 and Id like to expand them to about 512X1024.
Try using this method:
public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) {
if(bitmapToScale == null)
return null;
//get the original width and height
int width = bitmapToScale.getWidth();
int height = bitmapToScale.getHeight();
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(newWidth / width, newHeight / height);
// recreate the new Bitmap and set it back
return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true); }
refer to my answer in: ImageView OutofMemoryException
Use matrix to resize the bitmap.
Check this
Resize Bitmap
If you want to scale the Bitmap manually, there is a Bitmap.createScaledBitmap() method that you can use for this.
If you want the ImageView to handle this, you have to set the layout_width/layout_height to something other than wrap_content or it will always shrink to the size of the Bitmap. Then you need to change the scaleType attribute to a type that actually scales the bitmap.
The functionality to scale image up or down is readily available via android.graphics library:
Bitmap bitmapScaled = Bitmap.createScaledBitmap(Bitmap bitmapOrig, int widthNew, int heightNew, boolean filter);

Crop particular part of image in android

I want to crop Red part from following image, Is there any simple method available in android that can crop following image.
I have found many SO questions but all are suggesting to used following code:
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100);
This code work well if width & height are around 2MP resolution, but if that cropped part is more than 3MP resolution than application got crashed with OOM error.
Is there any way that handle image more than 3MP during cropping?
You can used following code that can solve your problem.
Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);
Above method do postScalling of image before cropping, so you can get best result with cropped image without getting OOM error.
For more detail you can refer this blog
1- Change your imageview for bitmap
final Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.img);
2-use your bitmap to crop what you want
Bitmap croppedBmp = Bitmap.createBitmap(bitmap, x, y , width , height);
3-Take care x,y from Top and left
4- to preview your bitmap again in your imageview
imageView.setImageBitmap(croppedBmp);
If you want to crop image in any shape OR only selected part then
you can use ready made open source library

set image size to full screen in android

I'm having an android app in which I'm taking a picture using the android camera.
This picture is taken in the activity A and after that is sent to activity B where is edited.
This is how I receive my image in activity B:
Bundle extras = getIntent().getExtras();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 5;
byte[] imageData = extras.getByteArray("imageData");
Bitmap myImage = BitmapFactory.decodeByteArray(imageData , 0, imageData.length,options);
Matrix mat=new Matrix();
mat.postRotate(90);
bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(), myImage.getHeight(), mat, true);
ImageView imageView = (ImageView) findViewById(R.id.myPic);
imageView.setImageBitmap(bitmapResult);
As you can see I'm rotating the bitmap I receive with 90 using this:
Matrix mat=new Matrix();
mat.postRotate(90);
And here is my imageView in xml file:
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/myPic"
/>
The problem that I'm facing is that my picture is not full screen...it's almoust full screnn but not entirly.As you can see here:
If you could help me to increase a little bit its size I would really apreciate it.Thanks
EDIT:I wanna increase the width of my image only.Sorry!!
You can use ImageView.ScaleType
ImageView.ScaleType="CENTER_CROP"
You might have to crop the image. Because the image is not at its full height, the width of the image is proportionately scaled down, hence the black strips at the sides.
To be more specific, images taken using the phone's camera are probably intended to fit the screen exactly i.e. width-height ratio of image = width-height ratio of the screen. However, in your app the height of the image is constrained by the buttons at the top, so in order to maintain the width-height ratio of the image, the width of the image is scaled down proportionately.
Please refer to setScaleType
You could use CENTER_CROP to get full screen image and maintain the image's aspect ratio.

Categories

Resources