Android: Bitmap_NJI - Failed To Create SkBitmap - java

So I have a function that loads an image using decodeFile - then I am resizing the photo and drawing it into a canvas. This works really well on my Moto Droid 2.2.3 & my Nexus 4.1.1 - but on my HTC 4.0.3 every time the createScaledBitmap fires it crashes and throws the error "Failed To Create SkBitmap"...
here is a sample of the code...
// create image bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inPurgeable = true;
bmOptions.inSampleSize = 2;
Bitmap rawImage = BitmapFactory.decodeFile(imageURL,bmOptions);
Bitmap bmp = Bitmap.createBitmap(rawImage);
float ratio = 0;
int maxHeight = 900;
int maxWidth = 900;
float height = bmp.getHeight();
float width = bmp.getWidth();
Log.d("HEIGHT",""+height);
Log.d("WIDTH",""+width);
int nh = 0;
int nw = 0;
// check if current width is larger
if(width > maxWidth){
ratio = maxWidth / width;
float newWidth = maxWidth;
float newHeight = height*ratio;
nw = Math.round(newWidth);
nh = Math.round(newHeight);
Log.d("NEW RATIO",""+ratio);
Log.d("NEW HEIGHT",""+newHeight);
// RESET HEIGHT AND WIDTH
height = height * ratio;
width = width * ratio;
}
if(height > maxHeight){
ratio = maxHeight / height;
float newWidth = width*ratio;
float newHeight = maxHeight;
nw = Math.round(newWidth);
nh = Math.round(newHeight);
Log.d("NEW RATIO",""+ratio);
Log.d("NEW HEIGHT",""+newHeight);
height = height * ratio;
width = width * ratio;
}
bmp = Bitmap.createScaledBitmap(bmp,nw,nh,false);
Bitmap img = Bitmap.createBitmap(bmp,0,0,655,655);
Like I said this works fine on my other devices and versions of Android... I tried searching but I cannot seem to find anything that really addresses this issue...
I know it is the createScaledBitmap because if I comment that line out everything works - but my bitmap doesn't scale.

To answer this -
The real issue was that on that particular device the resizing ratio was too much so rather than fit into the defined 655 area the image was slightly too small - which was causing a resize error but on the HTC device running 4.0.3 it was throwing the "Failed To Create SkBitmap" error.
The solution was to adjust the dimensions of the resized image.

Related

Scaling down and making image square in android

I need a way to scale an image down to 78x78. I have found ways of doing this by cutting part of the image off, like this:
Bitmap image = Bitmap.createBitmap(image, 0, 0, 78, 78);
but I need to maintain as much of the image as possible. I had thought of scaling the image down and then making it square:
Bitmap image = Bitmap.createScaledBitmap(imageTest, 78, 78, true);
but of course this creates a square image that is squashed.
Can anyone suggest how I can create a 78x78 image that doesn't rescale and maintains as much of the original image as possible?
From what I understood, you should scale down and center crop the image. Try this code out.
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
Hope it helps
Try this:
Bitmap image = Bitmap.createScaledBitmap(testImage, (int) 78 * (testImage.getWidth() / testImage.getHeight()), 78, true);
image = Bitmap.createBitmap(image, (int) (image.getWidth() - 78) / 2, 78);
Haven't tested this, as I'm on my way to bed, but it should accomplish what you want, so long as your image has a width greater than or equal to its height.
Regardless, I'd suggest you use BufferedImage instead of Bitmap.
The idea here would be resize your image using the same resize rate for width and height keeping the smaller size in 78. After that you can use a center point based crop to get the middle of your image and making it a squared image.
Image srcImage;
int widthSrc = 150;
int heightSrc = 180;
float resizeRate = 78 / min(widthSrc, heightSrc);
Image resizedImage = resizeImage($srcImage, resizeRate);
int widthDest = 78;
int heightDest = 78;
int cropX = ($widthSrc - $widthDest)/2;
int cropY = ($heightSrc - $heightDest)/2;
Image croppedImage = cropImage(resizedImage,$widthDest, $heightDest, $cropX, $cropY);
If the image is already square you can skip the crop part.

create bitmap to screen size keeping ratio

I'm trying to make a Wallpaper Application. And I had big trouble during set wallpaper with bitmap. I have tried to figure out the answer for a week.
I want to set Bitmap in to wallpaper like
avoid crop
scaleType:fit_center(align center vertical, keep original bitmap's ratio)
How can I make it? Do I have to create new bitmap?
You need to resize the picture to make a new bitmap according to your screen size.
Here's the code:
Bitmap bitmapOrg = BitmapFactory.decodeFile(getApplicationContext()
.getFilesDir().toString() + "/images/" + imagename);
Log.e("imageheight", "" + bitmapOrg.getHeight());
Log.e("imagewidth", "" + bitmapOrg.getWidth());
double imageheight = bitmapOrg.getHeight();
double imagewidth = bitmapOrg.getWidth();
DisplayMetrics metrics = getApplicationContext().getResources()
.getDisplayMetrics();
double screenwidth = metrics.widthPixels;
double sreeenheight = metrics.heightPixels;
Log.e("screennwidth", "" + screenwidth);
double newratio = screenwidth / imagewidth;
Log.e("newratio", "" + newratio);
double newratio1 = newratio * imageheight;
double newratio2 = newratio * (imagewidth - 10); // 10 margin in width
Log.e("newratio1", "" + newratio1);
int mainheight = (int) newratio1;
// int mainweidth = (int) imagewidth;
int mainweidth = (int) newratio2;
Log.e("Mainheight", "" + mainheight);
Log.e("Mainweidtht", "" + mainweidth);
// Here you will get the scaled bitmap
Bitmap new_ScaledBitmap = Bitmap.createScaledBitmap(bitmapOrg, mainweidth,mainheight, true);
// Use this bitmap as wallpaper
To fit the Bitmap to the screen without cutting anything of, you first have to decide if the aspect ratio is bigger than the one the screen has or smaller. If the image aspect ratio is bigger than the screen aspect ratio, that means the bitmap is higer and/or not as wide as the screen, like the second image in the question. So you should scale the image based on the height like this:
if(imageWidth/imageHeight > screenWidth/screenHeight){
scaleFactor = screenHeight/imageHeight;
imageXPosition = screenWidth/2-imageWidth/2;
imageYPosition = 0;
Else the image should be scaled based on the width like this:
}else{
scaleFactor = screenWidth/imageHeight;
imageXPosition = 0;
imageYPosition = screenWidth/2-imageWidth/2;
}
You can use these values to draw the bitmap using a Matrix or create a scaled Bitmap with the dimensions imageWidth*scaleFactor and imageHeight*scaleFactor and draw it at imageXPosition | imageYPosition (this is more memory saving.

How to get device raw width and height or actual screen width and height ?

I am trying to get the actual screen width and height of the Nexus 7 running on 4.2.
I am doing some runtime calculations based on device width and height to resize buttons and some UI elements, so it will look proper in all devices. my code was working great in all SDKs 4.1 and below but not working on 4.2.
I am using following code to get width and height.
Code: 1
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
mWidth = displaymetrics.widthPixels;
mHeight = displaymetrics.heightPixels;
Code: 2
Point size = new Point();
WindowManager w = getWindowManager();
w.getDefaultDisplay().getSize(size);
mWidth = size.x;
mHeight = size.y;
I have also tried with undocumented methods
Code: 3
Display display = getWindowManager().getDefaultDisplay();
Method mGetRawW = Display.class.getMethod("getRawWidth");
Method mGetRawH = Display.class.getMethod("getRawHeight");
mWidth = (Integer) mGetRawW.invoke(display);
mHeight = (Integer) mGetRawH.invoke(display);
But none of above is working with Nexus 7 running on 4.2, its always subtracting status bar height, I am not getting full height.
I have used some methods to calculate status bat height but not getting proper values,
int statusBarHeight = Math.ceil(25 * context.getResources().getDisplayMetrics().density);
AND
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
Is there any standard way to get actual device screen height and width?
Give it a try with the last changes of API 17 :
http://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)
public void getRealSize (Point outSize)
Gets the real size of the display without subtracting any window decor or applying any
compatibility scale factors.
If this does'nt work, you can try :
http://developer.android.com/reference/android/view/Display.html#getRealMetrics(android.util.DisplayMetrics)
int height = getResources().getDisplayMetrics().heightPixels;
int width = getResources().getDisplayMetrics().widthPixels;
height represents the device's screen height in pixels and
width represents the device's screen width in pixels.
Try this
int width = 0;
int height =0;
Display display = activity.getWindowManager().getDefaultDisplay();
if (Build.VERSION.SDK_INT >= 13) {
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
} else {
width = display.getWidth();
height = display.getHeight();
}

How to re-scale bitmap to different screen sizes but keep it square?

Hello great people of stackoverflow.com, I have a stumbled upon a great difficulty in my code; that being not being able to resize my images in-line with my screen size.
I have an image that is SAY 65 pixels by 65 pixels but I want this image to be a certain percentage of the screen say 6% while keeping square.
My brain cannot process the mathematics for this (because it is slow :-( )
(_width & _height is the screen width and height)
What I've got so far is:
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int avg_screen_dimension = (_width + _height) / 2;
float scaleWidth =
((float) width + (avg_screen_dimension - _width)) / width;
float scaleHeight =
((float) height + (avg_screen_dimension - _height)) / 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(bitmap, 0, 0, width, height, matrix, false);
return resizedBitmap;
Any help would be greatly appreciated!
It's little more than simple algebra - no complex math needed.
You'll have to choose between fraction of the width or height of your screen, unless they're equal.
Let h = screen height, w = screen width, f = fraction of screen dimension, x = image size.
Then x = h*f. If you want to make it a fraction of the width, substitute w for h.
(Just as an aside - in your pasted code, you've mixed up the variable names : you declare them as height and width but pass them as _width and _height

Resize image while keeping aspect ratio in Java

im trying to resize bufferdImage in memory in java but to keep the aspect ratio of the image
im have something like this but this is not good
int w = picture.getWidth();
int h = picture.getWidth();
int neww=w;
int newh=h;
int wfactor = w;
int hfactor = h;
if(w > DEFULT_PICTURE_WIDTH || h > DEFULT_PICTURE_HIGHT)
{
while(neww > DEFULT_PICTURE_WIDTH)
{
neww = wfactor /2;
newh = hfactor /2;
wfactor = neww;
hfactor = newh;
}
}
picture = Utils.resizePicture(picture,neww,newh);
Adding to Erik's point about getScaledInstance, if you moved away from it to using the recommended scaling mechanisms in Java2D, you might have noticed that your images look noticeably worse.
The reason for that is when the Java2D discouraged use of getScaledInstance and AreaAveragingScaleFilter, they didn't replace it with anything as easy to use in the API, instead we were left to our own devices using Java2D APIs directly. Fortunately, Chris Campbell (from the J2D team) followed up with the recommendation of using an incremental scaling technique that gives similar looking results to AreaAveragingScaleFilter and runs faster; unfortunately the code is of a decent size and doesn't address your original question of honoring proportions.
About 6 months ago I saw all these questions on SO again and again about "scaling images in Java" and eventually collected all the advice, did all the digging and research I could, and compiled all of into a single "best practices" image scaling library.
The API is dead simple as it is only 1 class and a bunch of static methods. Basic use looks like this:
BufferedImage img = ImageIO.read(...); // load image
BufferedImage scaledImg = Scalr.resize(img, 320);
This is the simplest call where the library will make a best-guess at the quality, honor your image proportions, and fit the result within a 320x320 bounding box. NOTE, the bounding box is just the maximum W/H used, since your image proportions are honored, the resulting image would still honor that, say 320x200.
If you want to override the automatic mode and force it to give you the best-looking result and even apply a very mild anti-alias filter to the result so it looks even better (especially good for thumbnails), that call would look like:
BufferedImage img = ImageIO.read(...); // load image
BufferedImage scaledImg = Scalr.resize(img, Method.QUALITY,
150, 100, Scalr.OP_ANTIALIAS);
These are all just examples, the API is broad and covers everything from super-simple use cases to very specialized. You can even pass in your own BufferedImageOps to be applied to the image (and the library automatically fixes the 6-year BufferedImageOp JDK bug for you!)
There is a lot more to scaling images in Java successfully that the library does for you, for example always keeping the image in one of the best supported RGB or ARGB image types while operating on it. Under the covers the Java2D image processing pipeline falls back to an inferior software pipeline if the image type used for any image operations is poorly supported.
If all that sounded like a lot of headache, it sort of is... that's why I wrote the library and open sourced it, so folks could just resize their images and move on with their lives without needing to worry about it.
If width, height of source and target are known, use following function to determine scale of the image.
private double determineImageScale(int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) {
double scalex = (double) targetWidth / sourceWidth;
double scaley = (double) targetHeight / sourceHeight;
return Math.min(scalex, scaley);
}
Then use this scale to scale up/down the image using following code
Image scaledImage = sourceBufferedImage.getScaledInstance((int) (width * scale), (int) (height * scale), Image.SCALE_SMOOTH);
For starters - take a look at line 2. Shouldnt that be getHeight()?
You dont want a while loop for the resizing, you want to find out the resizing ratio, which is a simple bit of math.
(width / height) = (new_width / new_height)
If you know one of the 'new' sizes, the other can be found via multiplication
new_height * (width / height) = new_width
You can also use the lazy method provided by BufferedImage's superclass Image, getScaledInstance() - using -1 for either width or height will maintain aspect ratio
ex:
scaledPic = picture.getScaledInstance(new_width, -1, Image.SCALE_FAST);
You may have a look at perils-of-image-getscaledinstance.html that explains why getScaledInstance(), used in some of the answers, should be avoided.
The article also provides alternative code.
I use these two methods to scale images, where max is the bigger dimension of your destination image. For 100x100 image it will be 100, for 200x300 image it will be 300.
public static BufferedImage scale(InputStream is, int max) {
Image image = null;
try {
image = ImageIO.read(is);
} catch (IOException e) {
e.printStackTrace();
}
int width = image.getWidth(null);
int height = image.getHeight(null);
double dWidth = 0;
double dHeight = 0;
if (width == height) {
dWidth = max;
dHeight = max;
}
else if (width > height) {
dWidth = max;
dHeight = ((double) height / (double) width) * max;
}
else {
dHeight = max;
dWidth = ((double) width / (double) height) * max;
}
image = image.getScaledInstance((int) dWidth, (int) dHeight, Image.SCALE_SMOOTH);
BufferedImage bImage = toBufferedImage(image);
return bImage;
}
public static BufferedImage toBufferedImage(Image img)
{
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return bimage;
}
If you want to resize a picture of w0 x h0 to w1 x h1 by keeping the aspect ratio, then calculate the vertical and horizontal scale and select the smaller one.
double scalex = 1;
double scaley = 1;
if (scalingMode == ScalingMode.WINDOW_SIZE) {
scalex = (double)getWidth() / frontbuffer.getWidth();
scaley = (double)getHeight() / frontbuffer.getHeight();
} else
if (scalingMode == ScalingMode.KEEP_ASPECT) {
double sx = (double)getWidth() / frontbuffer.getWidth();
double sy = (double)getHeight() / frontbuffer.getHeight();
scalex = Math.min(sx, sy);
scaley = scalex;
// center the image
g2.translate((getWidth() - (frontbuffer.getWidth() * scalex)) / 2,
(getHeight() - (frontbuffer.getHeight() * scaley)) / 2);
}
g2.scale(scalex, scaley);
if (interpolation != ImageInterpolation.NONE) {
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolation.hint);
}
g2.drawImage(frontbuffer, 0, 0, null);
private static BufferedImage resize(BufferedImage img, int width, int height) {
double scalex = (double) width / img.getWidth();
double scaley = (double) height / img.getHeight();
double scale = Math.min(scalex, scaley);
int w = (int) (img.getWidth() * scale);
int h = (int) (img.getHeight() * scale);
Image tmp = img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
BufferedImage resized = new BufferedImage(w, h, img.getType());
Graphics2D g2d = resized.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return resized;
}

Categories