在android中,如何通过编程将我的图片大小从70 kb减小到30 kb? [英] In android how can i reduce my image size from 70 kb to 30 kb programatically?

查看:61
本文介绍了在android中,如何通过编程将我的图片大小从70 kb减小到30 kb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用android开发一个应用程序,该应用程序将捕获照片并将其存储在sqlite中.图像大小为70 kb,但我想以35 kb的大小存储该图像.但是我对此一无所知.我尝试了以下代码,但没有成功.

I am developing an app in android that will capture a photo and store in sqlite. The image size is 70 kb but i want to store that image with 35 kb size. But i can't get any idea for this. I tried the following code but without success.

int photo_width = bm.getWidth();
int photo_height = bm.getHeight();

photo_width = 260;
photo_height = 260;

Bitmap photobitmap = Bitmap.createScaledBitmap(bm,
    photo_width, photo_height, false);

推荐答案

使用此选项:

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;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);

    return resizedBitmap;
}

您可以使用这一个...这是最好的例子.....

you can use this one...This is the Best Example.....

  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);

    //The new size we want to scale to
    final int REQUIRED_SIZE=70;

    //Find the correct scale value. It should be the power of 2.
    int scale=1;
    while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
        scale*=2;

    //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;
  }

这篇关于在android中,如何通过编程将我的图片大小从70 kb减小到30 kb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆