How to pixelize a bitmap in android - android

I have been searching but i did not find a question about how to pixelize a bitmap in android
Example
Note that i dont mean blur

You could just try and resize that image to a smaller version (and then back up if you need it at the same size as the original for some reason)
{
Bitmap original = ...;
//Calculate proportional size, or make the method accept just a factor of scale.
Bitmap small = getResigetResizedBitmap(original, smallWidth, smallHeight);
Bitmap pixelated = getResigetResizedBitmap(small, normalWidth, normalHeight);
//Recycle small, recycle original if no longer needed.
}
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
The code is from here

The simplest way to pixelate the image would be to scale image down using "nearest neighbour" algorithm, and then scale up, using the same algorithm.
Filtering over the image trying to find an average takes much more time, but does not actually give any improvements in result quality, after all you do intentionally want your image distorted.

Related

How to resize image without loosing image content

I want to resize an image to a smaller image like thumbnail. I have used following options
Bitmap imageBitmap = Bitmap.createScaledBitmap(mBitmap, 200, 700, false);
and
ThumbnailUtils.extractThumbnail()
Both of these options cut parts of images. So they don't look good. I just want to reduce image dimensions and don't want to loose image content.
you have to try like this
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(filepath,bmOptions);
//Part 1 :withcompression Sacle image 200 pixels x 700 pixels this size you can customize as per your requirement
Bitmap withCompressed = Bitmap.createScaledBitmap(bitmap,200,700,true);
If you want to resize an image without losing quality and images are from drawable/mipmap then you should try pngquant.
https://pngquant.org/

how to resize the image and its quality not be change in android?

I am creating an application and want to setup Image . I do not want the images in same size, but same image quality. how to resize that image but same image quality in android ?
i'm using this method to resize the image
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
.getAbsolutePath());
int h = myBitmap.getHeight() / 20;
int w = myBitmap.getWidth() / 20;
Bitmap scaled = Bitmap.createScaledBitmap(myBitmap, w, h,
true);
imageTab.setImageBitmap(scaled);
Pass btimap you want to resize and size in which you want it to resize in following method
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
Your requirement makes no sense. Whenever you resize it the quality will be changed. Downscaling will make your image lose image data like thin lines etc, and upscaling will make your image blurred and lose clarity.
This is because you only have so much image data with you, and you cannot arbitrarily change the amount of data you have to produce good quality images.
That said, using a format like PNG over JPG can help you save some of the quality at least, as JPG compresses the data even more.
Here is code snippet ..
private static BufferedImage resizeImage(BufferedImage originalImage, int type){
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
return resizedImage;
}
BufferedImage resizeImagePng = resizeImage(originalImage, type);
ImageIO.write(resizeImagePng, "png", new File("Image PAth"));

Zooming the Bitmap on Surfaceview in Android

I want to resize the bitmap slowly (like zooming in the bitmap )on surface view. When I set some standard increased values for width and height, it will displays the bigger image.As I give below in onDraw method
can.drawBitmap(gball, lastVisitedX, lastVisitedY, paint);
gball = getResizedBitmap(gball, bitmapInitHeight
+ bitmapInitHeight, bitmapInitHeight + bitmapInitHeight);
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
But when I try to resize by increasing the height and width like
can.drawBitmap(gball, lastVisitedX, lastVisitedY, paint);
gball = getResizedBitmap(gball, bitmapInitHeight
+ zoomXValue, bitmapInitHeight + zoomYValue);
zoomXValue= zoomXValue+5;
zoomYValue= zoomYValue+5;
In onDraw method but the bitmap is resize with bad quality.
Finally my question is how to scale like zooming out with out damaging the quality.
Thanks in Advance.
You are always going to have scaling problems especially if you scale up and you have same hard edges on your image, only way you can avoid this (in some manner) is to use larger image and always scale down (Still not going to help with hard edges).
Try setting Filter to true it might help a little :)
Hope this helps and enjoy your work.

Setting wallpaper

Im trying to set my bitmap image as a wallpaper!
Im currently using this code to get it done.
WallpaperManager wpm = WallpaperManager.getInstance(this);
float minH = wpm.getDesiredMinimumHeight();
float minW = wpm.getDesiredMinimumWidth();
Log.d("seb", minH + " = Min Height");
Log.d("seb", minW + " = Min Width");
targetBitmap = Bitmap.createScaledBitmap(targetBitmap,(int)minW, (int)minH, false);
wpm.setBitmap(targetBitmap);
It works! The phone automatically resizes the bitmap to fit the screen, but no mather how small my bitmap is, it's always cropped horizontally and scaled up.
Does anyone know how to fix this?
(One fix would be to put a black border around and have them cropped instead of the actual picture, though Im guessing there is a better alternative )
EDIT
this is original picture in code.
The following picture is what I mean with cropped when set to wallpaper:
And this will be in the same way even if i resize the image since the system automatically enlarges the picture to fit the whole screen
Ok, so after our discussion in "comments", here is a solution which can work.
Try that:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
Try it and if you have the same (or another) problem, let me know and we will try to fix it.

How to make an image small rather than its original size?

I am developing an animation app in the android.
i have one big image in application.
The image should start from the center of screen. The image size will be bigger at the initial stage. While moving to the left of the screen, its size should decrease(i.e. scaling should take place). Image should not go back to its original position. It should be placed at the leftside of the screen itself after the animation.
Can anyone please help.
Thanking you.
If I understand conrrectly you want to display an image and then animate it so that it appers to be reducing in size giving an affect that is going into the background.
You need to use ScaleAnimation
http://developer.android.com/reference/android/view/animation/ScaleAnimation.html
A good tutorial regarding android animation can be found at:
http://www.hascode.com/2010/09/playing-around-with-the-android-animation-framework/#Scale_Animations
this function resize the bitmap
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(90);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 70, bytes);
return resizedBitmap;
}

Categories

Resources