如何获得完全清晰的图像?

时间:2014-03-13 10:44:50

标签: android bitmap bitmapimage android-bitmap

我尝试从sdcard相机文件夹加载图片,加载后我使用缩放位图创建了位图,因为我只想显示图像的某些部分,所以我尝试使用createBitmap()创建一个新的位图,但它仍然是变得模糊,我该如何解决这个问题?

    File[] files = targetDirector.listFiles();
            for (File file : files) {

                String path = file.getAbsolutePath();
                itemList.add(path);


                if (myBitmap != null) {
                    myBitmap = null;
                }

                myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

                System.out.println("thumbnails1 width: " + myBitmap.getWidth());
                System.out.println("thumbnails1 height: " + myBitmap.getHeight());
                myBitmap = Bitmap.createScaledBitmap(myBitmap, 150, 150, false);

                ExifInterface exif;
                try {
                    exif = new ExifInterface(file.getAbsolutePath());

                    String orientString = exif
                            .getAttribute(ExifInterface.TAG_ORIENTATION);

                    int orientation = orientString != null ? Integer
                            .parseInt(orientString)
                            : ExifInterface.ORIENTATION_NORMAL;
                    int rotationAngle = 0;

                    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                        System.out.println("photopotart");
                        rotationAngle = 90;

                        System.out.println("myBitmap" + myBitmap.getWidth());
                        System.out.println("myBitmap" + myBitmap.getHeight());

                         myBitmap = Bitmap.createScaledBitmap(myBitmap, 150, 150,
                         false);



                        Matrix matrix = new Matrix();
                        matrix.postRotate(90);
                        myBitmap = Bitmap.createBitmap(myBitmap, 0, 0,
                                150, 150, matrix,
                                true);

                        myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, 150, 100 );

                    myBitmap = highlightImage(myBitmap);

                        photsList.add(myBitmap);
                    }

                    else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                        System.out.println("180 angle");
                        rotationAngle = 180;

                    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                        System.out.println("other 270");
                        rotationAngle = 270;
                    } else {
                        System.out.println("photolandscape");
                    myBitmap = Bitmap.createScaledBitmap(myBitmap, 150, 150,
                                false);


                        myBitmap = Bitmap.createBitmap(myBitmap, 30, 0, 120, 150);



                        photsList.add(myBitmap);
                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }








            }
        }

我的xml文件是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="150dip"
        android:layout_height="150dip" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

使用以下方法解码文件路径。

 public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

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

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;

    int scale = 2;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(filePath, o2); // this bmp object of Bitmap is global and you can set it to your ImageView.
    }

现在你可以用这个函数调用这个函数替换

   myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

   decodeFile(file.getAbsolutePath());

您可以根据您的要求设置比例变量,以便您可以非常清晰地获得图像。