How to fix image banding in Android? - android

I have tried setting dithering everywhere I can find. I also tried setting everything to ARGB_8888 and STILL there is very bad banding on my background image. My background image is 640x960 and it works fine on my physical phone that is 720x1280 but on an emulator running at 320x480 I get the bad color banding. I put my code below. If you have any suggestions please help!
public void onCreate(Bundle instanceBundle) {
super.onCreate(instanceBundle);
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.setFormat( PixelFormat.RGBA_8888 );
surfaceView = new SurfaceView(this);
setContentView(surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.setFormat( PixelFormat.RGBA_8888 );
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
if(width > height) {
setOffscreenSurface(height, width);
} else {
setOffscreenSurface(width, height);
}
surfaceView.setFocusableInTouchMode(true);
surfaceView.requestFocus();
surfaceView.setOnKeyListener(this);
background = decodeSampledBitmapFromResource( getResources(), R.drawable.background );
}
public Bitmap decodeSampledBitmapFromResource( Resources res, int resId )
{
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options );
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
options.inDither = true;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeResource(res, resId, options);
}
public int calculateInSampleSize( BitmapFactory.Options options )
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
int reqWidth = Math.round( width * vertDispRatio );
int reqHeight = Math.round( height * horiDispRatio );
if( height > reqHeight || width > reqWidth )
{
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round( (float) height / (float) reqHeight );
final int widthRatio = Math.round( (float) width / (float) reqWidth );
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
public void setOffscreenSurface(int width, int height) {
if(offscreenSurface != null) offscreenSurface.recycle();
offscreenSurface = Bitmap.createBitmap(width, height, Config.ARGB_8888);
canvas = new Canvas(offscreenSurface);
}
Paint paint = new Paint();
public void drawBitmap(Bitmap bitmap, float x, float y)
{
if(canvas != null)
{
int pixelX = (int)( x * getFramebufferWidth() );
int pixelY = (int)( y * getFramebufferHeight() );
paint.setDither(true);
canvas.drawBitmap(bitmap, pixelX, pixelY, paint);
}
}
public void run() {
int frames = 0;
long startTime = System.nanoTime();
long lastTime = System.nanoTime();
while(true) {
if( state == State.Running )
{
if( !surfaceHolder.getSurface().isValid() )
continue;
Canvas canvas = surfaceHolder.lockCanvas();
long currTime = System.nanoTime();
float deltaTime = (currTime - lastTime) / 1000000000.0f;
if( deltaTime > 0.1f )
deltaTime = 0.1f;
clearFramebuffer( Color.BLACK );
drawBitmap( background, 0, 0 );
src.left = 0;
src.top = 0;
src.right = offscreenSurface.getWidth() - 1;
src.bottom = offscreenSurface.getHeight() - 1;
dst.left = 0;
dst.top = 0;
dst.right = surfaceView.getWidth();
dst.bottom = surfaceView.getHeight();
canvas.drawBitmap(offscreenSurface, src, dst, null);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}

I dont see anywhere in your code where your trying to set anti aliasing. You can also do that with the paint you use in your canvas. that should help and if not maybe the device you are using just sucks? :P
Edit:
I swore that you could set anti-aliasing through bitmapfactory but I guess not. You can definitely do it from paint though. You can set it by using the paint.setAntiAlias(aa) method.

Related

Bitmap.createScaledBitmap not resizing the image twice the original size

I am trying to resize an image twice its original size.
Say, a width of 623 and height of 415.
So i tried this code:
Bitmap resized_captured_image_bm =
Bitmap.createScaledBitmap(ogBitmap,(int)(ogBitmap.getWidth() * 2),
(int)(ogBitmap.getHeight() * 2), false);
But the above code does nothing to the image, the displayed image is still the same dimension size.
Try to use next method:
public static final int MAX_FILE_ONE_SIZE = 200 * 1024;
public static final int MAX_SIZE_1 = 1280;
public static final int MAX_SIZE_2 = 960;
public static Bitmap resizeBitmap(Context context, Bitmap bitmap) {
Bitmap result = null;
if(bitmap != null){
int cnt = 0;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
result = bitmap.copy(bitmap.getConfig(), true);
while(getImageSize(result) >= MAX_FILE_ONE_SIZE || !canSendBitmapResolution(result)){
result.recycle();
width = width/2;
height = height/2;
result = Bitmap.createScaledBitmap(bitmap, width, height, true);
if(cnt++ > 10) {
result = null;
break;
}
}
bitmap.recycle();
}
return result;
}
public static boolean canSendBitmapResolution(Bitmap bitmap){
boolean result = false;
if((bitmap.getWidth() <= MAX_SIZE_1 && bitmap.getHeight() <= MAX_SIZE_2)
|| (bitmap.getWidth() <= MAX_SIZE_2 && bitmap.getHeight() <= MAX_SIZE_1)){
result = true;
}
return result;
}
The problem is that the scale ratio doesn't match.
you just need to figure out what ratio to use and scale accordingly.
Ex:
final int maxSize = 960;
int outWidth;
int outHeight;
int inWidth = myBitmap.getWidth();
int inHeight = myBitmap.getHeight();
if(inWidth > inHeight){
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, outWidth, outHeight, false);
or, follow by google docs: Loading large bitmaps

how to reduce image Size without Quality break in android

Bitmap bmp = getResizedBitmap(
BitmapFactory.decodeByteArray(data, 0, data.length),
500);
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 0) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
This is my code Using getResizedBitmap i able to reduced image size but unable to keep its original quality in android please tell me how to keep quality Good so that image Size reduce and Quality should not bad please suggest me !
hi please try below code hope it meets which you want
public static Bitmap scaleImage(String p_path, int p_reqHeight, int p_reqWidth) throws Throwable
{
Bitmap m_bitMap = null;
System.gc();
File m_file = new File(p_path);
if (m_file.exists())
{
BitmapFactory.Options m_bitMapFactoryOptions = new BitmapFactory.Options();
m_bitMapFactoryOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
m_bitMapFactoryOptions.inSampleSize = calculateInSampleSize(m_bitMapFactoryOptions, p_reqHeight, p_reqWidth);
m_bitMapFactoryOptions.inJustDecodeBounds = false;
m_bitMap = BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
}
else
{
throw new Throwable(p_path + " not found or not a valid image");
}
return m_bitMap;
}
// Helper method
private static int calculateInSampleSize(BitmapFactory.Options p_options, int p_reqWidth, int p_reqHeight)
{
// Raw height and width of image
final int m_height = p_options.outHeight;
final int m_width = p_options.outWidth;
int m_inSampleSize = 1;
if (m_height > p_reqHeight || m_width > p_reqWidth)
{
final int m_halfHeight = m_height / 2;
final int m_halfWidth = m_width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((m_halfHeight / m_inSampleSize) > p_reqHeight && (m_halfWidth / m_inSampleSize) > p_reqWidth)
{
m_inSampleSize *= 2;
}
}
return m_inSampleSize;
}

Re sizing bitmap before sending to Wallpapermanager

I am needing some help with resizing a bitmap before sending it to the wallpaper manager so that when the user sets it as their wallpaper, it fits reasonably, 100% would be preferred.
I am using wallpaper manager and am getting the image from an ImageView.
The issue I am having is the wallpaper is really zoomed in. Before, when I set the wallpaper straight from the drawable directory, it looked fine and you could see a lot more of the image, not 1/4 of it. I have changed my code up since then and have found a lot more of an effective way to get my images and set the wallpaper.
I have looked at This link here and am trying to figure out how to implement the answer that shows you how to resize the image before sending it to the wallpaper manager.
Any help would be appreciated, cheers.
Relative code to question:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.image_detail_fragment,
container, false);
int Measuredwidth = 0;
int Measuredheight = 0;
WindowManager w = getActivity().getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(Size);
Measuredwidth = Size.x;
Measuredheight = Size.y;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();
}
mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
BitmapDrawable drawable = (BitmapDrawable) mImageView
.getDrawable();
Bitmap bitmap = drawable.getBitmap();
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getActivity());
try {
myWallpaperManager.setBitmap(bitmap);
;
Toast.makeText(getActivity(),
"Wallpaper Successfully Set!", Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
Toast.makeText(getActivity(), "Error Setting Wallpaper",
Toast.LENGTH_LONG).show();
}
}
My whole class:
public class ImageDetailFragment extends Fragment {
private static final String IMAGE_DATA_EXTRA = "extra_image_data";
private static final Point Size = null;
private String mImageUrl;
private RecyclingImageView mImageView;
private ImageFetcher mImageFetcher;
public static ImageDetailFragment newInstance(String imageUrl) {
final ImageDetailFragment f = new ImageDetailFragment();
final Bundle args = new Bundle();
args.putString(IMAGE_DATA_EXTRA, imageUrl);
f.setArguments(args);
return f;
}
public ImageDetailFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mImageUrl = getArguments() != null ? getArguments().getString(
IMAGE_DATA_EXTRA) : null;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.image_detail_fragment,
container, false);
int Measuredwidth = 0;
int Measuredheight = 0;
WindowManager w = getActivity().getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(Size);
Measuredwidth = Size.x;
Measuredheight = Size.y;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();
}
mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
BitmapDrawable drawable = (BitmapDrawable) mImageView
.getDrawable();
Bitmap bitmap = drawable.getBitmap();
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getActivity());
try {
myWallpaperManager.setBitmap(bitmap);
;
Toast.makeText(getActivity(),
"Wallpaper Successfully Set!", Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
Toast.makeText(getActivity(), "Error Setting Wallpaper",
Toast.LENGTH_LONG).show();
}
}
});
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (Batmanark.class.isInstance(getActivity())) {
mImageFetcher = ((Batmanark) getActivity()).getImageFetcher();
mImageFetcher.loadImage(mImageUrl, mImageView);
}
}
#Override
public void onDestroy() {
super.onDestroy();
if (mImageView != null) {
// Cancel any pending image work
ImageWorker.cancelWork(mImageView);
mImageView.setImageDrawable(null);
}
}
}
if you want to fit the wallpaper with the divice screen, then you have to follow the steps bellow:
get the height and width of the divice screen
sample the bitmap image
resize the bitmap
before setting the bitmap as wallpaper, recycle the previous bitmap
code:
step 1:
int Measuredwidth = 0;
int Measuredheight = 0;
Point size = new Point();
// if you are doing it from an activity
WindowManager w = getWindowManager();
// otherwise use this
WindowManager w = context.getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
Measuredwidth = size.x;
Measuredheight = size.y;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();
}
step 2+3:
public Bitmap resizeBitmap(Resources res, int reqWidth, int reqHeight,
InputStream inputStream, int fileLength) {
Bitmap bitmap = null;
InputStream in = null;
InputStream in2 = null;
InputStream in3 = null;
try {
in3 = inputStream;
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
copy(in3,out,fileLength);
out2 = out;
in2 = new ByteArrayInputStream(out.toByteArray());
in = new ByteArrayInputStream(out2.toByteArray());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
if(options.outHeight == -1 || options.outWidth == 1 || options.outMimeType == null){
return null;
}
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(in2, null, options);
if(bitmap != null){
bitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, false);
}
in.close();
in2.close();
in3.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee a final image
// with both dimensions larger than or equal to the requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
// This offers some additional logic in case the image has a strange
// aspect ratio. For example, a panorama may have a much larger
// width than height. In these cases the total pixels might still
// end up being too large to fit comfortably in memory, so we should
// be more aggressive with sample down the image (=larger inSampleSize).
final float totalPixels = width * height;
// Anything more than 2x the requested pixels we'll sample down further
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
}
return inSampleSize;
}
public int copy(InputStream input, OutputStream output, int fileLength) throws IOException{
byte[] buffer = new byte[8*1024];
int count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
publishProgress((int) (count * 100 / fileLength));
}
return count;
}
step 4:
to recycle the bitmap use:
bitmap.recycle();
bitmap = null;
call the function like resizeBitmap(context.getResources(), Measuredwidth, Measuredheight,
THE_INPUTSTREAM_FROM_WHERE_YOU_ARE_DOWNLOADING_THE_IMAGE,
FILELENGTH_FROM_THE_INPUTSTREAM);.
if you are calling the function from an activity the call it like: resizeBitmap(getResources(), Measuredwidth, Measuredheight,
THE_INPUTSTREAM_FROM_WHERE_YOU_ARE_DOWNLOADING_THE_IMAGE, FILELENGTH_FROM_THE_INPUTSTREAM);
the function will return resized bitmap which will fit with the divice resulation.
if you have already setted a bitmap as wallpaper, then don't forget to recycle the bitmap before you set a new bitmap as wallpaper.
Please see the function and change the size according to your need. Thanks
public Bitmap createScaledImage(Bitmap bit) {
Bitmap bitmapOrg = bit;
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 0, newHeight = 0;
if (MyDevice.getInstance().getDeviceSize().equals("XLARGE")) {
MyDevice.getInstance().SCALE = 65;
newWidth = 65;
newHeight = 65;
} else if (MyDevice.getInstance().getDeviceSize().equals("LARGE")) {
MyDevice.getInstance().SCALE = 60;
newWidth = 60;
newHeight = 60;
}
else if (MyDevice.getInstance().getDeviceSize().equals("NORMAL")) {
MyDevice.getInstance().SCALE = 50;
newWidth = 50;
newHeight = 50;
if (h > 800) {
MyDevice.getInstance().SCALE = 60;
newWidth = 60;
newHeight = 60;
}
} else if (MyDevice.getInstance().getDeviceSize().equals("SMALL")) {
MyDevice.getInstance().SCALE = 30;
newWidth = 30;
newHeight = 30;
}
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
height, matrix, true);
return resizedBitmap;
}
Where MyDevice is a singleton class here. You can change it as you want. getdevicesize method determines what device it is.

Image is not scaled to full screen

I am trying to decode the image before drawing it in Canvas. The procedure I followed is same as mentioned in http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Issue :
The image is scaled only to 70% of the screen. Can you please let me know if I miss any parameters ?
public Bitmap getAssetImage(Context context, String filename) throws IOException {
//Decode image size
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.backgroundhomepage, options);
//The new size we want to scale to
final int REQUIRED_WIDTH= (int) dWidth;
final int REQUIRED_HIGHT= (int) dHeight;
//Find the correct scale value. It should be the power of 2.
int inSampleSize=1;
if (options.outHeight > REQUIRED_HIGHT || options.outWidth > REQUIRED_WIDTH) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) options.outHeight / (float) REQUIRED_HIGHT);
final int widthRatio = Math.round((float) options.outWidth / (float) REQUIRED_WIDTH);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
/*
while(options.outWidth/inSampleSize/2>=REQUIRED_WIDTH && options.outHeight/inSampleSize/2>=REQUIRED_HIGHT)
inSampleSize*=2; */
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=inSampleSize;
o2.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.backgroundhomepage, o2);
}
public void run() {
// TODO Auto-generated method stub
// Log.i("Notice", "In run of mybringback");
if(backgoundImage == null){
try {Log.i("MyBringBack", "In run of mybringback.. getting the image of background");
backgoundImage = getAssetImage(getApplicationContext(),"backgroundhomepage");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ourHolder = getHolder();
while (isRunning) {
// Log.i("DragDrop", "ourHolder.getSurface().isValid()" + ourHolder.getSurface().isValid() );
if (!ourHolder.getSurface().isValid()){
continue;
}
canvas = ourHolder.lockCanvas();
screenCenterX = dWidth / 2;
screenCenterY = dHeight / 2;
canvas.drawBitmap(backgoundImage, 0, 0, null);
if (imagePublishDone) {
if(!welcomeDone){
message = "Drop your wish to panda";
tts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
welcomeDone=true;
}
moveImageInEllipticalPath();
} else {
initialImagePublish();
}
centreReached = false;
ourHolder.unlockCanvasAndPost(canvas);
}
}
final int REQUIRED_WIDTH= Screen_width;
final int REQUIRED_HIGHT= screen_height;
Calculate screen dimensions first
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screen_width = size.x;
int screen_height = size.y;

Scaled Bitmap maintaining aspect ratio

I would like to scale a Bitmap to a runtime dependant width and height, where the aspect ratio is maintained and the Bitmap fills the entire width and centers the image vertically, either cropping the excess or filling in the gap with 0 alpha pixels.
I'm currently redrawing the bitmap myself by creating a Bitmap of all 0 alpha pixels and drawing the image Bitmap on top of it, scaling to the exact specified width and maintaining the aspect ratio, however, it ends up losing/screwing up the pixel data.
Here is how I'm doing it:
Bitmap background = Bitmap.createBitmap((int)width, (int)height, Config.ARGB_8888);
float originalWidth = originalImage.getWidth(), originalHeight = originalImage.getHeight();
Canvas canvas = new Canvas(background);
float scale = width/originalWidth;
float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale)/2.0f;
Matrix transformation = new Matrix();
transformation.postTranslate(xTranslation, yTranslation);
transformation.preScale(scale, scale);
canvas.drawBitmap(originalImage, transformation, null);
return background;
Is there a library out there or some better code that can do this better? I would like the image to look as crisp as possible, but I knew that my function wouldn't provide a great result.
I know I could have the image stay fine by using integer scaling, instead of float scaling, but I need the width to be 100% filled.
Also, I know about an ImageView's Gravity.CENTER_CROP capability, however, that also uses integer scaling, so it cuts off the width of the image when it shouldn't.
This will respect maxWidth and maxHeight, which means the resulting bitmap will never have dimensions larger then those:
private static Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {
if (maxHeight > 0 && maxWidth > 0) {
int width = image.getWidth();
int height = image.getHeight();
float ratioBitmap = (float) width / (float) height;
float ratioMax = (float) maxWidth / (float) maxHeight;
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax > ratioBitmap) {
finalWidth = (int) ((float)maxHeight * ratioBitmap);
} else {
finalHeight = (int) ((float)maxWidth / ratioBitmap);
}
image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
return image;
} else {
return image;
}
}
What about this:
Bitmap background = Bitmap.createBitmap((int)width, (int)height, Config.ARGB_8888);
float originalWidth = originalImage.getWidth();
float originalHeight = originalImage.getHeight();
Canvas canvas = new Canvas(background);
float scale = width / originalWidth;
float xTranslation = 0.0f;
float yTranslation = (height - originalHeight * scale) / 2.0f;
Matrix transformation = new Matrix();
transformation.postTranslate(xTranslation, yTranslation);
transformation.preScale(scale, scale);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(originalImage, transformation, paint);
return background;
I added a paint to filter the scaled bitmap.
here is a method from my Utils class, that does the job:
public static Bitmap scaleBitmapAndKeepRation(Bitmap targetBmp,int reqHeightInPixels,int reqWidthInPixels)
{
Matrix matrix = new Matrix();
matrix .setRectToRect(new RectF(0, 0, targetBmp.getWidth(), targetBmp.getHeight()), new RectF(0, 0, reqWidthInPixels, reqHeightInPixels), Matrix.ScaleToFit.CENTER);
Bitmap scaledBitmap = Bitmap.createBitmap(targetBmp, 0, 0, targetBmp.getWidth(), targetBmp.getHeight(), matrix, true);
return scaledBitmap;
}
Here I have a tested solution where I create a scaled Bitmap out of a bitmap file:
int scaleSize =1024;
public Bitmap resizeImageForImageView(Bitmap bitmap) {
Bitmap resizedBitmap = null;
int originalWidth = bitmap.getWidth();
int originalHeight = bitmap.getHeight();
int newWidth = -1;
int newHeight = -1;
float multFactor = -1.0F;
if(originalHeight > originalWidth) {
newHeight = scaleSize ;
multFactor = (float) originalWidth/(float) originalHeight;
newWidth = (int) (newHeight*multFactor);
} else if(originalWidth > originalHeight) {
newWidth = scaleSize ;
multFactor = (float) originalHeight/ (float)originalWidth;
newHeight = (int) (newWidth*multFactor);
} else if(originalHeight == originalWidth) {
newHeight = scaleSize ;
newWidth = scaleSize ;
}
resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);
return resizedBitmap;
}
Notice that I need scaled Bitmaps which have a maximum size of 4096x4096 Pixels but the aspect ratio needs to be kept while resizing. If you need other values for width or height just replace the values "4096".
This is just an addition to the answer of Coen but the problem in his code is the line where he calculates the ratio. Dividing two Integers gives an Integer and if the result is < 1 it will be rounded to 0. So this throws the "divide by zero" exception.
None of the above answers worked for me and I just created a method that sets all of the dimensions into the desired ones by painting the empty area black. Here is my method:
/**
* Scale the image preserving the ratio
* #param imageToScale Image to be scaled
* #param destinationWidth Destination width after scaling
* #param destinationHeight Destination height after scaling
* #return New scaled bitmap preserving the ratio
*/
public static Bitmap scalePreserveRatio(Bitmap imageToScale, int destinationWidth,
int destinationHeight) {
if (destinationHeight > 0 && destinationWidth > 0 && imageToScale != null) {
int width = imageToScale.getWidth();
int height = imageToScale.getHeight();
//Calculate the max changing amount and decide which dimension to use
float widthRatio = (float) destinationWidth / (float) width;
float heightRatio = (float) destinationHeight / (float) height;
//Use the ratio that will fit the image into the desired sizes
int finalWidth = (int)Math.floor(width * widthRatio);
int finalHeight = (int)Math.floor(height * widthRatio);
if (finalWidth > destinationWidth || finalHeight > destinationHeight) {
finalWidth = (int)Math.floor(width * heightRatio);
finalHeight = (int)Math.floor(height * heightRatio);
}
//Scale given bitmap to fit into the desired area
imageToScale = Bitmap.createScaledBitmap(imageToScale, finalWidth, finalHeight, true);
//Created a bitmap with desired sizes
Bitmap scaledImage = Bitmap.createBitmap(destinationWidth, destinationHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(scaledImage);
//Draw background color
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
//Calculate the ratios and decide which part will have empty areas (width or height)
float ratioBitmap = (float)finalWidth / (float)finalHeight;
float destinationRatio = (float) destinationWidth / (float) destinationHeight;
float left = ratioBitmap >= destinationRatio ? 0 : (float)(destinationWidth - finalWidth) / 2;
float top = ratioBitmap < destinationRatio ? 0: (float)(destinationHeight - finalHeight) / 2;
canvas.drawBitmap(imageToScale, left, top, null);
return scaledImage;
} else {
return imageToScale;
}
}
For example;
Let's say you have an image as 100 x 100 but the desired size is 300x50, then this method will convert your image to 50 x 50 and paint it into a new image that has dimensions of 300 x 50 (and empty fields will be black).
Another example: let's say you have an image as 600 x 1000 and the desired sizes are 300 x 50 again, then your image will be converted into 30 x 50 and painted into a newly created image which has sizes of 300 x 50.
I think this is what it must be, Rs.
simpler solution : note we set the width to 500 pixels
public void scaleImageKeepAspectRatio()
{
int imageWidth = scaledGalleryBitmap.getWidth();
int imageHeight = scaledGalleryBitmap.getHeight();
int newHeight = (imageHeight * 500)/imageWidth;
scaledGalleryBitmap = Bitmap.createScaledBitmap(scaledGalleryBitmap, 500, newHeight, false);
}
It can also be done by calculating the ratio yourself, like this.
private Bitmap scaleBitmap(Bitmap bm) {
int width = bm.getWidth();
int height = bm.getHeight();
Log.v("Pictures", "Width and height are " + width + "--" + height);
if (width > height) {
// landscape
int ratio = width / maxWidth;
width = maxWidth;
height = height / ratio;
} else if (height > width) {
// portrait
int ratio = height / maxHeight;
height = maxHeight;
width = width / ratio;
} else {
// square
height = maxHeight;
width = maxWidth;
}
Log.v("Pictures", "after scaling Width and height are " + width + "--" + height);
bm = Bitmap.createScaledBitmap(bm, width, height, true);
return bm;
}
Added RESIZE_CROP to Gowrav's answer.
enum RequestSizeOptions {
RESIZE_FIT,
RESIZE_INSIDE,
RESIZE_EXACT,
RESIZE_CENTRE_CROP
}
static Bitmap resizeBitmap(Bitmap bitmap, int reqWidth, int reqHeight, RequestSizeOptions options) {
try {
if (reqWidth > 0 && reqHeight > 0 && (options == RequestSizeOptions.RESIZE_FIT ||
options == RequestSizeOptions.RESIZE_INSIDE ||
options == RequestSizeOptions.RESIZE_EXACT || options == RequestSizeOptions.RESIZE_CENTRE_CROP)) {
Bitmap resized = null;
if (options == RequestSizeOptions.RESIZE_EXACT) {
resized = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, false);
} else {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scale = Math.max(width / (float) reqWidth, height / (float) reqHeight);
if (scale > 1 || options == RequestSizeOptions.RESIZE_FIT) {
resized = Bitmap.createScaledBitmap(bitmap, (int) (width / scale), (int) (height / scale), false);
}
if (scale > 1 || options == RequestSizeOptions.RESIZE_CENTRE_CROP) {
int smaller_side = (height-width)>0?width:height;
int half_smaller_side = smaller_side/2;
Rect initialRect = new Rect(0,0,width,height);
Rect finalRect = new Rect(initialRect.centerX()-half_smaller_side,initialRect.centerY()-half_smaller_side,
initialRect.centerX()+half_smaller_side,initialRect.centerY()+half_smaller_side);
bitmap = Bitmap.createBitmap(bitmap, finalRect.left, finalRect.top, finalRect.width(), finalRect.height(), null, true);
//keep in mind we have square as request for cropping, otherwise - it is useless
resized = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, false);
}
}
if (resized != null) {
if (resized != bitmap) {
bitmap.recycle();
}
return resized;
}
}
} catch (Exception e) {
Log.w("AIC", "Failed to resize cropped image, return bitmap before resize", e);
}
return bitmap;
}
Kotlin extension function version based on joaomgcd's answer
private fun Bitmap.resize(maxWidth: Int, maxHeight: Int): Bitmap {
return if (maxHeight > 0 && maxWidth > 0) {
val width = this.width
val height = this.height
val ratioBitmap = width.toFloat() / height.toFloat()
val ratioMax = maxWidth.toFloat() / maxHeight.toFloat()
var finalWidth = maxWidth
var finalHeight = maxHeight
if (ratioMax > ratioBitmap) {
finalWidth = (maxHeight.toFloat() * ratioBitmap).toInt()
} else {
finalHeight = (maxWidth.toFloat() / ratioBitmap).toInt()
}
Bitmap.createScaledBitmap(this, finalWidth, finalHeight, true)
} else this
}
This is an awesome library from ArthurHub to handle the image crops both programmatically and interactively if you don't want to reinvent the wheel.
But if you prefer a non bloated version like me.., the internal function shown here is a rather sophisticated to perform Image Scaling with few standard options
/**
* Resize the given bitmap to the given width/height by the given option.<br>
*/
enum RequestSizeOptions {
RESIZE_FIT,
RESIZE_INSIDE,
RESIZE_EXACT
}
static Bitmap resizeBitmap(Bitmap bitmap, int reqWidth, int reqHeight, RequestSizeOptions options) {
try {
if (reqWidth > 0 && reqHeight > 0 && (options == RequestSizeOptions.RESIZE_FIT ||
options == RequestSizeOptions.RESIZE_INSIDE ||
options == RequestSizeOptions.RESIZE_EXACT)) {
Bitmap resized = null;
if (options == RequestSizeOptions.RESIZE_EXACT) {
resized = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, false);
} else {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scale = Math.max(width / (float) reqWidth, height / (float) reqHeight);
if (scale > 1 || options == RequestSizeOptions.RESIZE_FIT) {
resized = Bitmap.createScaledBitmap(bitmap, (int) (width / scale), (int) (height / scale), false);
}
}
if (resized != null) {
if (resized != bitmap) {
bitmap.recycle();
}
return resized;
}
}
} catch (Exception e) {
Log.w("AIC", "Failed to resize cropped image, return bitmap before resize", e);
}
return bitmap;
}
public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) {
float originalWidth = bitmap.getWidth();
float originalHeight = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Matrix m = new Matrix();
float scalex = wantedWidth/originalWidth;
float scaley = wantedHeight/originalHeight;
float xTranslation = 0.0f, yTranslation = (wantedHeight - originalHeight * scaley)/2.0f;
m.postTranslate(xTranslation, yTranslation);
m.preScale(scalex, scaley);
// m.setScale((float) wantedWidth / bitmap.getWidth(), (float) wantedHeight / bitmap.getHeight());
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(bitmap, m, paint);
return output;
}
My solution was this, which maintains aspect ratio, and requires only one size, for example if you have a 1920*1080 and an 1080*1920 image and you want to resize it to 1280, the first will be 1280*720 and the second will be 720*1280
public static Bitmap resizeBitmap(final Bitmap temp, final int size) {
if (size > 0) {
int width = temp.getWidth();
int height = temp.getHeight();
float ratioBitmap = (float) width / (float) height;
int finalWidth = size;
int finalHeight = size;
if (ratioBitmap < 1) {
finalWidth = (int) ((float) size * ratioBitmap);
} else {
finalHeight = (int) ((float) size / ratioBitmap);
}
return Bitmap.createScaledBitmap(temp, finalWidth, finalHeight, true);
} else {
return temp;
}
}
There is simple math involved in rescaling the image, consider the following snippet and follow along,
1. Suppose, you have Imaan image with 720x1280 and you want to to be fit in 420 width, get the percentage of reduction required by given math,
originalWidth = 720;
wP = 720/100;
/* wP = 7.20 is a percentage value */
Now subtract the required width from original width and then multiply the outcome by wP. You will get the percentage of width being reduced.
difference = originalWidth - 420;
dP = difference/wP;
Here dP will be 41.66, means you are reducing the size 41.66%. So you have to reduce the height by 41.66(dP) to maintain the ration or scale of that image.
Calculate the height as given below,
hP = originalHeight / 100;
//here height percentage will be 1280/100 = 12.80
height = originalHeight - ( hp * dP);
// here 1280 - (12.80 * 41.66) = 746.75
Here is your fitting scale, you can resize image/Bitmap in 420x747. It will return the resized image without losing the ratio/scale.
Example
public static Bitmap scaleToFit(Bitmap image, int width, int height, bool isWidthReference) {
if (isWidthReference) {
int originalWidth = image.getWidth();
float wP = width / 100;
float dP = ( originalWidth - width) / wP;
int originalHeight = image.getHeight();
float hP = originalHeight / 100;
int height = originalHeight - (hP * dP);
image = Bitmap.createScaledBitmap(image, width, height, true);
} else {
int originalHeight = image.getHeight();
float hP = height / 100;
float dP = ( originalHeight - height) / hP;
int originalWidth = image.getWidth();
float wP = originalWidth / 100;
int width = originalWidth - (wP * dP);
image = Bitmap.createScaledBitmap(image, width, height, true);
}
return image;
}
here you are simply scaling the image with reference of height or width parameter to fit into required criteria.

Categories

Resources