I'm trying to rotate and then scale an image using android.graphics.Matrix. The code is as follows:
int width = bmp1.getWidth();
int height = bmp1.getHeight();
int smallerVal = (height > width) ? width : height;
int n = 0;
while (smallerVal > 1)
{
smallerVal = smallerVal >> 1;
n++;
}
int pow2 = (int) Math.pow(2, n);
float scaleWidth = ((float) AppBase.width) / (float)pow2;
float scaleHeight = ((float) AppBase.height) / (float)pow2;
Matrix matrix = new Matrix();
matrix.postRotate(90);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bmp2 = Bitmap.createBitmap(bmp1, 0, 0, pow2, pow2, matrix, true);
pow2 is to get a square power-of-2 image for the sake of applying a texture. bmp2 turns out to have -1 as height and width, ie an invalid image. Am I missing something?
Thanks,
Rajath
The solution I'm using for my problem is below, in case anyone needs it in future.
int pow2 = getSquareVal(bmp1);
int width = bmp1.getWidth();
int height = bmp1.getHeight();
float scaleWidth = ((float) pow2) / (float)width;
float scaleHeight = ((float) pow2) / (float)height;
Matrix matrix = new Matrix();
matrix.reset();
matrix.postScale(scaleWidth, scaleHeight);
if (width > height)
matrix.postRotate(90);
bmp2 = Bitmap.createBitmap(bmp1, 0, 0, width, height, matrix, true);
Related
i have try to remove the black background after the rotate image in my code
Bitmap rotate(float x, Bitmap bitmapOrg) {
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 200;
int newHeight = 200;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(x);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,height, matrix, true);
//Canvas canvas = new Canvas(resizedBitmap);
//canvas.drawColor(Color.TRANSPARENT);
return resizedBitmap;
}
how can i remove black background i have seen that only in my android lolipop , i also try in my emulator Google galaxy nexus - 4.2.2 in that black background is not display .
You must use resizedBitmap.setHasAlpha(true);
put that line of code right after you declare your bitmap.
I take a picture with camera through intent. After I receive the image, my app has a rotate button. say the image came back width = 900 and height=1200. After I rotate the image, I still want width = 900 and height=1200 as opposed to width = 1200 and height=900. Does anyone know how I might to that?
Here is the code that hasn't worked.
Bitmap bmp = readBitmap(context.getContentResolver(), imageUri, sampleScale, options);
float widthScale = 0.9f * deviceDimension[1] / bmp.getWidth();
float heightScale = 0.8f * deviceDimension[0] / bmp.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(widthScale, heightScale);
matrix.preRotate(angle);//90x
return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
So basically, I want the image to still fit a present dimension on the screen no matter how I orient the image. The present area is, say, 900 by 1200
Use
return Bitmap.createBitmap(bmp, 0, 0, bmp.getHeight(), bmp.getWidth(), matrix, true);
instead of
return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
see if this works??
public static Bitmap ScaleBitmap(Bitmap bm, float scalingFactor) {
int scaleHeight = (int) (bm.getHeight() * scalingFactor);
int scaleWidth = (int) (bm.getWidth() * scalingFactor);
return Bitmap.createScaledBitmap(bm, scaleWidth, scaleHeight, true);
}
private float getBitmapScalingFactor(Bitmap bm) {
// Get display width from device
int displayWidth = getWindowManager().getDefaultDisplay().getWidth();
// Get margin to use it for calculating to max width of the ImageView
LinearLayout.LayoutParams layoutParams =
(LinearLayout.LayoutParams)this.imageView.getLayoutParams();
int leftMargin = layoutParams.leftMargin;
int rightMargin = layoutParams.rightMargin;
// Calculate the max width of the imageView
int imageViewWidth = displayWidth - (leftMargin + rightMargin);
// Calculate scaling factor and return it
return ( (float) imageViewWidth / (float) bm.getWidth() );
}
I am new in bitmap.I know how can i resize or scale bitmap in android.But problem is suppose my image is 100x500 or any height & width.Now i want to resize it in square like 100x100.How it is possible
Kindly help me.
For this simple case, the most reasonable thing would be to translate your source image down to the middle, and draw your Bitmap again on a new Canvas. This type of resize is called a center crop in Android. The idea of a center crop is to result in the largest image that fills the entire bounds, and does not change the aspect ratio.
You can implement this yourself, along with other types of resizing and scaling. Basically, you use a Matrix to post your changes, like scaling and moving (translating), and then draw your original Bitmap on a Canvas that takes the Matrix into account.
Here's a method I adopted from another answer here (can't find the original post to properly give credit):
public static Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth)
{
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
//get the resulting size after scaling
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
//figure out where we should translate to
float dx = (newWidth - scaledWidth) / 2;
float dy = (newHeight - scaledHeight) / 2;
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
matrix.postTranslate(dx, dy);
canvas.drawBitmap(source, matrix, null);
return dest;
}
int dstWidth = 100;
int dstHeight = 100;
boolean doFilter = true;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, doFilter);
Made a few modifications to the code from wsanville..and it worked for me
Note that I am using the minimum scale (taking what is the least, so that the whole bitmap can be rendered in the screen..if I take max, then it might go beyond the screen
int sourceWidth = mBitmap.getWidth();
int sourceHeight = mBitmap.getHeight();
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.min(xScale, yScale);
//get the resulting size after scaling
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
//figure out where we should translate to
float dx = (newWidth - scaledWidth) / 2;
float dy = (newHeight - scaledHeight) / 2;
Matrix defToScreenMatrix = new Matrix();
defToScreenMatrix.postScale(scale, scale);
defToScreenMatrix.postTranslate(dx, dy);
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, sourceWidth, sourceHeight, defToScreenMatrix, false);
I have one image, image width is 290 and height is 330.when i used bitmap factory for convert into bitmap width and height is increasing.My code is,
mILearnBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.capital_a_1);
mILearnBitmap = mILearnBitmap.copy(Bitmap.Config.ARGB_8888, true);
mILearnPixelsArry = new int[mILearnBitmap.getWidth() * mILearnBitmap.getHeight()];
mILearnBitmap.getPixels(mILearnPixelsArry, 0, mILearnBitmap.getWidth(), 0, 0,mILearnBitmap.getWidth(), mILearnBitmap.getHeight());
Log.d("mILearnPixelsArry", "---" + mILearnPixelsArry.length);
Log.d("Width", "---" + mILearnBitmap.getWidth());
Log.d("Height", "---" + mILearnBitmap.getHeight());
After using this code width increased to 435 and height increased to 495.Please tell i made mistake some there please help me.
please use this function to resize image
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);
return resizedBitmap;
}
You can use this -
Bitmap.createScaledBitmap(bm, int destinationWidth, int destinationHeight, boolean filter);
I've been trying this for some time, I would like to create a wallpaper from a Bitmap. Let's say the desired wallpaper size is 320x480, and the source image size is 2048x2048.
I'm not sure whether crop-to-fit is the right term, but what I would like to achieve is to get most part of the picture that has the equal ratio as the desired wallpaper size (320x480).
So in this case, I would like to get 2048x1365 or (1365.333... to be exact) from the source Bitmap, and scale it down to 320x480.
The technique that I have tried is:
1) Crop the Bitmap into 2048x1365 first
bm = Bitmap.createBitmap(bm, xOffset, yOffset, 2048, 1365);
2) Scale it down to 320x480
bm = Bitmap.createScaledBitmap(bm, 320, 480, false);
which produced OutOfMemory error.
Is there any way to achieve this?
Regards,
dezull
Thanks to open source, I found the answer from Android Gallery source code here at line 230 :-D
croppedImage = Bitmap.createBitmap(mOutputX, mOutputY, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(croppedImage);
Rect srcRect = mCrop.getCropRect();
Rect dstRect = new Rect(0, 0, mOutputX, mOutputY);
int dx = (srcRect.width() - dstRect.width()) / 2;
int dy = (srcRect.height() - dstRect.height()) / 2;
// If the srcRect is too big, use the center part of it.
srcRect.inset(Math.max(0, dx), Math.max(0, dy));
// If the dstRect is too big, use the center part of it.
dstRect.inset(Math.max(0, -dx), Math.max(0, -dy));
// Draw the cropped bitmap in the center
canvas.drawBitmap(mBitmap, srcRect, dstRect, null);
I know this is an incredibly late reply, but something like this maybe:
public static Bitmap scaleCropToFit(Bitmap original, int targetWidth, int targetHeight){
//Need to scale the image, keeping the aspect ration first
int width = original.getWidth();
int height = original.getHeight();
float widthScale = (float) targetWidth / (float) width;
float heightScale = (float) targetHeight / (float) height;
float scaledWidth;
float scaledHeight;
int startY = 0;
int startX = 0;
if (widthScale > heightScale) {
scaledWidth = targetWidth;
scaledHeight = height * widthScale;
//crop height by...
startY = (int) ((scaledHeight - targetHeight) / 2);
} else {
scaledHeight = targetHeight;
scaledWidth = width * heightScale;
//crop width by..
startX = (int) ((scaledWidth - targetWidth) / 2);
}
Bitmap scaledBitmap = Bitmap.createScaledBitmap(original, (int) scaledWidth, (int) scaledHeight, true);
Bitmap resizedBitmap = Bitmap.createBitmap(scaledBitmap, startX, startY, targetWidth, targetHeight);
return resizedBitmap;
}
here is an answer that gets you most of the way there:
How to crop an image in android?