【问题标题】:poor image quality after saving rotated bitmap to sdcard将旋转位图保存到 SD 卡后图像质量不佳
【发布时间】:2013-12-20 04:48:19
【问题描述】:

我正在制作一个应用程序,在其中一项活动中我从图库中获取图像并将其显示在适配器中,如下图所示

我必须旋转该图像并将其保存到 SD 卡。我的代码运行良好,但将其保存到 sdcard 后,图像质量非常差。我的代码是:

 viewHolder.imgViewRotate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            imagePosition = (Integer) v.getTag();
            Matrix matrix = new Matrix();
            matrix.postRotate(90);

            Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

            try {
                FileOutputStream out = new FileOutputStream(new File(uriList.get(rotatePosition).toString()));
                rotated.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            notifyDataSetChanged();

        }
    });

任何建议都会有很大帮助。

【问题讨论】:

  • 尝试将压缩比从 100 降低到 50
  • 图像仍然失真
  • @Raghunandan 有什么建议吗??

标签: 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;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

已编辑:

使用BitmapFactory inSampleSize 选项调整图像大小,图像完全不会丢失质量。代码:

          BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
              bmpFactoryOptions.inJustDecodeBounds = true;
              Bitmap bm = BitmapFactory.decodeFile(tempDir+"/"+photo1_path , bmpFactoryOptions);

              int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)600);
              int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)800);

              if (heightRatio > 1 || widthRatio > 1)
              {
               if (heightRatio > widthRatio){
                bmpFactoryOptions.inSampleSize = heightRatio;
               } else {
                bmpFactoryOptions.inSampleSize = widthRatio; 
               } 
              }

              bmpFactoryOptions.inJustDecodeBounds = false;
              bm = BitmapFactory.decodeFile(tempDir+"/"+photo1_path, bmpFactoryOptions);

              // recreate the new Bitmap
        src = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(), bm.getHeight(), matrix, true);
        src.compress(Bitmap.CompressFormat.PNG, 100, out);

【讨论】:

  • 我不必减小图像的大小,我只需旋转它并将旋转后的位图保存到 sdcard
  • 那你的问题标题误导了别人。
  • 它工作了一点,但静止图像有点失真。
  • Bitmap.CompressFormat.PNG的问题是文件每次旋转都会变大。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-04
相关资源
最近更新 更多