1

i'm having Quality issue after rescaling bitmap i load The images from assets so decoding them have to done by Stream

here is my method

  InputStream is = getAssets().open(lol.get(zpositions));   
Bitmap bm = BitmapFactory.decodeStream(is);
  Bitmap mBitmap    = Bitmap.createScaledBitmap(bm,width,hight, false);

i tried

Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPreferredConfig = Config.ARGB_8888;

Bitmap bitmapsrc = BitmapFactory.decodeStream(is, null, options);
Bitmap mBitmap  = Bitmap.createScaledBitmap(bitmapsrc,width,hight, false);

but still same issue

any idea how to bypass this issue? thanks

here is the code

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="1dip"
android:background="@android:color/black" >

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
   />

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullimage);

    Display display = getWindowManager().getDefaultDisplay(); 
       width = display.getWidth();  
       height = display.getHeight(); 

       AdView adView = (AdView)findViewById(R.id.ad);
        adView.loadAd(new AdRequest());

     Intent i = getIntent();
    List<String> lol = i.getStringArrayListExtra("lol");
    this.lol = lol;
     pagerPosition = i.getExtras().getInt("id");

 {

 public Bitmap getbitBitmap() throws IOException{
    positions = MyPageChangeListener.getCurrentPage();


    if (positions == 0) {
         zpositions = pagerPosition;

    }
    InputStream is = getAssets().open(lol.get(zpositions)); 
            Options options = new BitmapFactory.Options();
            options.inScaled = false;

    Bitmap bitmapsrc = BitmapFactory.decodeStream(is, null, options);


    return bitmapsrc;

}

          private void CropWallpaper() throws IOException {
      Bitmap mBitmap    = Bitmap.createScaledBitmap(bitmapsrc,width, height, false);    
        try {
            setWallpaper(mBitmap);
            Toast.makeText(this,"Cropped successfully" ,      Toast.LENGTH_SHORT).show();   
        } catch (Exception e) {
            Toast.makeText(this,"Failed to Crop" , Toast.LENGTH_SHORT).show();
            // TODO: handle exception
        }
}
Joseph27
  • 129
  • 3
  • 16
  • Are you trying to increase the size, or decrease? Increasing will inherently have negative effects on quality. Decreasing will also, to some extent, but these can normally be solved by playing with aliasing. – dberm22 Apr 11 '13 at 20:18
  • We need to know the width and height of the resource image in your "assets" folder AND the width and height that you are sending to `Bitmap.createScaledBitmap()`. – David Manpearl Apr 11 '13 at 20:20
  • Have you checked these? http://stackoverflow.com/questions/2041207/android-quality-of-the-images-resized-in-runtime http://stackoverflow.com/questions/4231817/quality-problems-when-resizing-an-image-at-runtime?rq=1 – Ross Aiken Apr 11 '13 at 20:32
  • i'm trying to resize the image to fit the screen size i know the bitmap width/height also screen width/size but using Bitmap.createScaledBitmap() decrease the quality after resizing that's my problem – Joseph27 Apr 11 '13 at 21:01

2 Answers2

0

Image quality is always visibly degraded to some amount when increasing the size of an image. Therefore, the resolution of your resource image in your "assets" folder is smaller than the screen on which you are displaying it when you scale the image to "fit the screen size".

The solution is to use an image at least the size of the largest screen on which you plan to display it. You can do this in two ways:

  1. Find the original source of the image that is now in your "assets" folder and resize it to the size of a relatively large android device screen, such as 1280x800. Don't go much larger, because you will get an OutOfMemoryError when you attempt to open the image.
  2. If your image is already small and you don't have access to a larger original copy, then I suggest resizing it in Photoshop or with another desktop imaging product before putting into your "assets" folder. These imaging products will do a better job of uprezing your image than the Android OS can do in real-time.

Finally, if you experience an OutOfMemoryError, your options will be to either:

  1. Reduce the size of your image until you find an acceptable balance between size and display quality, or
  2. Move your image from the "assets" folder into several different size images in the standard "drawable-hdpi", "drawable-mdpi", etc. directory structure. The OS will open an image in one of these directories which is in accordance with screen density that is more likely to be proportional to the Heap Memory capabilities of the hardware.

Of course, you can also use BitmapFactory.Options such as 32 bit (ARGB444) and dither when up-scaling, but this will only help so much when increasing the size of your image for display.

David Manpearl
  • 12,362
  • 8
  • 55
  • 72
  • well i'm using HD Quality and still bad quality and i'm using memory/disk cashe so outofmemoryerror is rare – Joseph27 Apr 11 '13 at 23:58
  • but how i can use bitmapfactory options with stream decode and then rescaling ? – Joseph27 Apr 11 '13 at 23:58
  • Sidetrack for a moment, but this is probably more important than `BitmapFactory.Options`. I don't think you should scale your `Bitmap` at all in your app. Once any `Bitmap` is open, use your layout to control the display scaling (i.e. XML `android:layout_width="match_parent"`). You may also have to use `android:scaleType="fitCenter"` or similar. So, go back to the beginning, open your image from the `InputStream` and display it as per this comment. You may also want to use `Bitmap.Config.ARGB_8888`. – David Manpearl Apr 12 '13 at 00:08
  • i want to scale images so i can set it as wallpaper i don't think that will work with setting wallpaper – Joseph27 Apr 12 '13 at 08:16
  • You're welcome. You can set `RGB_8888` in your options before this: `Bitmap bitmapsrc = BitmapFactory.decodeStream(is, null, options);`. But, your best improvement will be to put larger images in your "assets" as recommended in the answer. – David Manpearl Apr 12 '13 at 16:55
  • i did exactly that but after rescaling bitmapsrc image quality turns so bad – Joseph27 Apr 12 '13 at 20:07
  • Here is a repost of a question I asked you 23 hours ago. It is time to provide the answers: _"We need to know the width and height of the resource image in your "assets" folder AND the width and height that you are sending to Bitmap.createScaledBitmap()."_ – David Manpearl Apr 12 '13 at 20:11
  • Please also show the layout XML for display of the image. Edit your question. – David Manpearl Apr 12 '13 at 20:13
  • Try `getWindow().setFormat(PixelFormat.RGBA_8888);` – David Manpearl Apr 12 '13 at 20:15
0

Since you have tried all combinations of BitmapOptions, try setting the filter parameter when creating the scaled bitmap.

Bitmap mBitmap  = Bitmap.createScaledBitmap(bitmapsrc,width,hight, true);

It smooths out the images when up-scaled.

Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63