Santosh
Santosh

Reputation: 201

Image Resizing issue

I am resizing a image according to this link.
Image size is 3264x2448 and after calculating the samplesize is now 24 but image is rotating 90degree(left).
It is happening when width is greater than height.
I am stucked. Can't resolve it.

I am using accroding to http://developer.android.com/training/displaying-bitmaps/load-bitmap.html


public static String getCompressedImagePath(String orgImagePath,
        String storeImagePath) {
    if (orgImagePath == null) {
        return null;
    }
    Bitmap bitmap = decodeSampledBitmapFromResource(orgImagePath, 100, 100);
    String absolutePath = "";
    FileOutputStream fos = null;
    try {

        fos = new FileOutputStream(storeImagePath);
        bitmap.compress(getCompressionFormatType(orgImagePath),
                IMAGE_COMPRESS_FACTOR, fos);
        fos.flush();
        absolutePath = storeImagePath;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
                fos = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return absolutePath;
}

public static Bitmap decodeSampledBitmapFromResource(String orgImagePath,
        int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(orgImagePath, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(orgImagePath, options);
}

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;


    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

And decodeSampledBitmapFromResource method has been copied from the link and setting the compressed image into intoimageview.

Upvotes: 0

Views: 1398

Answers (1)

Pushpan
Pushpan

Reputation: 277

Well I used your code & used in my sample application & it works fine. So you run this application & find out what else you are missing

ACTIVITY-ImageScaleTestActivity


    package com.myTutororial;

    import java.io.File;

    import com.myTutororial.utils.ImageUtils;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;

    public class ImageScaleTestActivity extends Activity {
        final String SRC_IMAGE = "/sdcard/Gallary/2.png";
        final String CONVERTED_IMAGE = "/sdcard/Gallary/Converted/1.png";
        boolean isConvertedImage;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button button = (Button) findViewById(R.id.button1);
            button.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    isConvertedImage= !isConvertedImage;
                    if(isConvertedImage){
                        setImage(CONVERTED_IMAGE);
                    }else{
                        setImage(SRC_IMAGE);
                    }
                }
            });
            setImage(SRC_IMAGE);
            ImageUtils.getCompressedImagePath(SRC_IMAGE, CONVERTED_IMAGE);
        }

        public void setImage(String imagePath) {
            File imgFile = new File(imagePath);
            if (imgFile.exists()) {

                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
                        .getAbsolutePath());

                ImageView myImage = (ImageView) findViewById(R.id.imageView1);
                myImage.setImageBitmap(myBitmap);

            }

        }
    }

XML-main.xml


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:src="@drawable/ic_launcher" android:layout_weight="1"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show converted Image" />

    </LinearLayout>

IMAGEUTILS


    package com.myTutororial.utils;

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import android.graphics.Bitmap;
    import android.graphics.Bitmap.CompressFormat;
    import android.graphics.BitmapFactory;

    public class ImageUtils {
        public static String getCompressedImagePath(String orgImagePath,
                String storeImagePath) {
            if (orgImagePath == null) {
                return null;
            }
            Bitmap bitmap = decodeSampledBitmapFromResource(orgImagePath, 100, 100);
            String absolutePath = "";
            FileOutputStream fos = null;
            try {

                fos = new FileOutputStream(storeImagePath);
                bitmap.compress(CompressFormat.PNG, 90, fos);
                fos.flush();
                absolutePath = storeImagePath;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos != null) {
                        fos.close();
                        fos = null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return absolutePath;
        }

        public static Bitmap decodeSampledBitmapFromResource(String orgImagePath,
                int reqWidth, int reqHeight) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(orgImagePath, options);
            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(orgImagePath, options);
        }

        public static int calculateInSampleSize(BitmapFactory.Options options,
                int reqWidth, int reqHeight) {
            final int height = options.outHeight;
            final int width = options.outWidth;

            int inSampleSize = 1;
            if (height > reqHeight || width > reqWidth) {
                if (width > height) {
                    inSampleSize = Math.round((float) height / (float) reqHeight);
                } else {
                    inSampleSize = Math.round((float) width / (float) reqWidth);
                }
            }
            return inSampleSize;
        }

      }

Upvotes: 1

Related Questions