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.
Related
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.
I use the following method to resize images on Android.
public Bitmap resize(Bitmap img, int Width, int Height) {
int width = img.getWidth();
int height = img.getHeight();
int newWidth = (int) Width;
int newHeight = (int) Height;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
//matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
return resizedBitmap;
}
It works fine on most Android devices. But on some devices the resized image is not displaying. How might I resolve this?
You may create a scaled bitmap with
Bitmap bitmap = Bitmap.createScaledBitmap(b, width, height, true);
Here, width and height you can provide according to the device's screen size for different devices, by reading screen sizes programmatically. Here is how you can read the screen size of a device programmatically.
Most likely, on "some devices" and depending on the requested width and height, there's not enough memory to create the resized bitmap. Check (in debugger) if the resizedBitmap is null when the image "is not displaying".
I need to resize my image with custom size. The image is taken from device camera or gallery, i tired the below code, but the image is stretched, i need the image in square shape with out any stretch.
public Bitmap decodeSampledBitmapFromFile(Bitmap bm, int boundBoxInDp) {
boundBoxInDp=300;
int height = bm.getHeight();
int width = bm.getWidth();
float scaleWidth = ((float) boundBoxInDp) / width;
float scaleHeight = ((float) boundBoxInDp) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
If you already have a bitmap, you could use the following code to resize:
Bitmap originalBitmap = <original initialization>;
Bitmap resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false);
Or you can use following library for resize image
https://github.com/biokys/cropimage
This won't fit your image in a bounding box (the failure of which is presumably what you're calling "stretch"). It will not handle rectangular bitmaps in your square bounding box, nor will it handle images smaller than the bounding box particularly well. You probably want something like:
public Bitmap decodeSampledBitmapFromFile(Bitmap bm, int boundBoxInDp) {
boundSquareInPx=convertToPixels(300);
int maxDimen = Math.max(bm.getHeight(), bm.getWidth())
float scale = (maxDimen <= boundSquareInPx) ? 1 : boundSquareInPx / (float) maxDimen;
float scaleWidth = scale * bm.getWidth();
float scaleHeight = scale * bm.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, true);
return resizedBitmap;
}
Few notes: if you image is smaller than your bound it won't fit it- obvious modifications do that.
Secondly, dp != px; the Bitmap object returns px, so you're going to have to convert to px from dp (which is well documented elsewhere).
Use postTranslate(...) if you need to center the correspondingly cropped bitmap.
The documentation is here; this is allready the best library I know for resizing in Android- I've never needed anything else, and I've been in the game a while and work with this frequently.
If you need, in my opinion, the best introduction to working with the API efficiently: read the source code to ImageView and Drawable instances; a really worthwhile personal development exercise would be to use the SDK to implement a fading transition drawable that is center cropped, as that's rather annoyingly one of the only things missing from the Android library, and would involve a heck of a lot of the kind of coding you're trying to do above.
NB:
As you'll note, another answerer has pointed out the existence of createScaledBitmap, which is probably much clearer code; I just wanted to point out how what you were doing was basically right and how it could be improved.
Best.
Take a look at https://github.com/coomar2841/image-chooser-library/blob/d27b542d2487132b0150be382f39e9ef95aafe68/src/com/kbeanie/imagechooser/threads/MediaProcessorThread.java.
The method called compressAndSaveImage.
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.
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;
}