如何提高android中的图像质量..?

时间:2012-12-20 05:56:21

标签: android bitmap imageview decode

我正在做一个像应用程序一样的“图像编辑器”。我使用默认相机意图捕获图像。我曾经解析URI并将其设置为图像视图,如下所示:

imgCaptured.setImageURI(Uri.parse(filePath));

如果我使用这个原始图像,偶尔它会让我失去内存错误!所以我决定使用以下内容解码图像:

“来自stackoverflow”

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    preview_bitmap = BitmapFactory.decodeStream(is, null, options);


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 = 95;

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

如果我使用解码图像,则输出质量不佳。一些似乎是模糊的。我想要一个清晰的图像!将其设置为ImageView后。我需要拖放另一个视图。

  • 我如何实现上述目标?
  • 这样做的好方法是什么?

3 个答案:

答案 0 :(得分:0)

在循环中除以2的原因是什么? 试试这个

while (o.outWidth / scale  >= REQUIRED_SIZE
         && o.outHeight / scale  >= REQUIRED_SIZE)

您可能也希望使用此方法将图像缩放到视图的大小 http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap(android.graphics.Bitmap,int,int,boolean)

答案 1 :(得分:0)

将options.inSampleSize = 8更改为options.inSampleSize = 4.

答案 2 :(得分:0)

您使用的方法不完整,您必须添加一些数学函数,以保持图像的质量。

private static Bitmap decodeFile(File f) {
            Bitmap b = null;
            final int IMAGE_MAX_SIZE = 100;
            try {
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                FileInputStream fis = new FileInputStream(f);
                BitmapFactory.decodeStream(fis, null, o);
                fis.close();
                int scale = 1;
                if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                    scale = (int) Math.pow(
                            2.0,
                            (int) Math.round(Math.log(IMAGE_MAX_SIZE
                                    / (double) Math.max(o.outHeight, o.outWidth))
                                    / Math.log(0.5)));
                }
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                fis = new FileInputStream(f);
                b = BitmapFactory.decodeStream(fis, null, o2);
                fis.close();
            } catch (Exception e) {
                Log.v("Exception in decodeFile() ", e.toString() + "");
            }
            return b;
        }

请告诉我,如果它对您有用...... !!!! :)。

相关问题