【问题标题】:In android how can i reduce my image size from 70 kb to 30 kb programatically?在 android 中,如何以编程方式将图像大小从 70 kb 减小到 30 kb?
【发布时间】:2013-11-28 09:45:09
【问题描述】:

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

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

【问题讨论】:

标签: android


【解决方案1】:

使用这个:

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

你可以用这个……这是最好的例子……

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

【讨论】:

  • 如果您从相机拍摄了两张照片,那么在运行时您必须将其添加到位图数组列表中...
  • 你能帮帮我吗?
  • @kaveriPatel 你应该做什么..当你从相机捕捉图像时,你已经将该位图对象添加到你的数组列表中......如果有文件路径,那么很容易将该路径保存到临时文件....然后解码它
  • no..看看我从相机拍了一张照片,如果这张照片有 80 k.b.大小然后我希望该图像看起来像它,但它的大小是 35 k.b...
  • 没有。我使用压缩而不是内存大小来减少像素
【解决方案2】:

您可以使用Bitmap.compress() 生成压缩版本。

【讨论】:

  • 我用过... photobitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytearrayoutputsrteam);...但不工作
  • 使用 100 表示您正在使用最高质量进行压缩。
  • 那么我可以使用哪个值?
  • 0 - 100 之间的任何值,0 表示压缩为最小尺寸。所以你必须选择最适合你的形象。
  • 我想制作 35 k.b 的图像。固定大小
猜你喜欢
  • 2016-09-14
  • 2014-09-11
  • 1970-01-01
  • 2014-02-19
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
  • 2012-12-14
  • 2018-10-09
相关资源
最近更新 更多