0

I am trying scale an image of 1280x800 image to 1024x600 to fit it in Galaxy 7 inch tab. I am using function draw() from this tutorial.

in my XML file i declare an imageView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" 
        />

</RelativeLayout>

in my java file :

public class MainActivity extends Activity {

    TextView text;
    Button mybutton;
    Bitmap bitmap2;
    ImageView iview;
    int maxWidth ;
    int maxHeight ;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        Display display = getWindowManager().getDefaultDisplay();
        Point size= new Point();
        (display).getSize(size);
        maxWidth = size.x;
        maxHeight = size.y;

        Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.main);
        mybutton=(Button) findViewById(R.id.Send);

        iview=(ImageView)findViewById(R.id.imageView1);

        resizeImage1(bitmap);// set the image
    }

     public void resizeImage1(final Bitmap image)
        {
         float scaleFactor;
         Bitmap resizedImage = null;
            float screenAspect = (float)maxWidth / (float)maxHeight;
            if (screenAspect <= 2)
            {
                scaleFactor = (float)(maxWidth )/ (float)(image.getWidth());

                Toast.makeText(this, "i am here", Toast.LENGTH_LONG).show();
            }
            else
            {
                final int PLAYFIELD_HEIGHT = 10000;
                scaleFactor = (float)maxHeight / (float)PLAYFIELD_HEIGHT;
            }

            int newWidth = (int)(image.getWidth() * scaleFactor);
            int newHeight = (int)(image.getHeight() * scaleFactor);
            int destX = (image.getWidth() - newWidth) / 2;
            int destY = (image.getHeight() - newHeight) / 2;           

            resizedImage = Bitmap.createScaledBitmap( image, newWidth, newHeight, true);
            iview.setMaxHeight(newHeight);
            iview.setMaxWidth(newWidth);

            iview.setImageBitmap(resizedImage);

            iview.setVisibility(1);
        }    
}

Now before resizing when i try to get the image height and width it gives me 640x400 but in actual the image is of size 1280x800. After resizing it gives the blur image because the image is re size from 640x400 to 1024x600.i can't understand why it is happening.

Any help will be appreciated

After updating with new code:

public class MainActivity extends Activity {

    ImageView iview;
    int maxWidth ;
    int maxHeight ;

    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        Display display = getWindowManager().getDefaultDisplay();
        Point size= new Point();
        (display).getSize(size);
        maxWidth = size.x;
        maxHeight = size.y;
        File photos= new File("C:/Users/drive3/workspace/images");


        Bitmap bitmap = decodeFile(photos);
        bitmap = Bitmap.createScaledBitmap(bitmap,150, 150, true);
    }

    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);              
            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=40;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale++;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        }
        return super.onOptionsItemSelected(item);
    }    
}
onkar
  • 4,427
  • 10
  • 52
  • 89
User42590
  • 2,473
  • 12
  • 44
  • 85

1 Answers1

0

If I'm not mistaken, BitmapFactory automatically scales images to the target density. So that's why you get an image that is 640 x 400 instead of the expected 1280 x 800.

You better use BitmapFactory to your advantage, i.e. you instruct it to directly create an image with the desired size. That will be much more efficient and your app will be less likely to run into out-of-memory problems.

Have a look at this answer. It shows how the BitmapFactory options are used.

Update:

To prevent to default scaling by BitmapFactory, you can change your original code like this. Instead of:

Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.main);

Use:

BitmapFactory.Options o = new BitmapFactory.Options();
o.inScaled= 0;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.main, o);
Community
  • 1
  • 1
Codo
  • 75,595
  • 17
  • 168
  • 206
  • ok i cpied the code but i am receiving error at this line after calling decodeFile function – User42590 Jan 01 '13 at 12:59
  • Copying the code probably won't do it. Both your current code and the copied code need to be adapted to your requirements. So you should study what it does. Anyhow, without seeing your complete solution and having the exact error message, it's basically impossible to help you. – Codo Jan 01 '13 at 13:05
  • i uploaded my updated code . see code after bold line in above question – User42590 Jan 02 '13 at 04:52
  • Thanks for the code. But as I've told before: without the exact error message, I can't really help you. Instead, I've updated my answer and proposed a solution based on your original code. – Codo Jan 02 '13 at 08:31