6

基本上我正在尝试在 Android 应用程序中旋转位图(来自图像)。我想这样做的原因是从相机(通过意图)拍摄的照片即使是垂直拍摄的也会水平显示,并且方向作为图像上的元数据保留。如果有错误请纠正我。然而,问题是,如果在带有相当好的相机的手机上拍摄图像,加载时会占用大量内存,而且我还没有找到一种方法来旋转和保存它而不会有出现 OutOfMemoryError 的风险. 下面的代码是我的地方:

  1. 加载图像
  2. 检查是否需要旋转
  3. 加载缩小版本以在 ImageView 中显示
  4. 必要时旋转小图像
  5. 在一个单独的线程中;加载,旋转和保存图像,因此以后不需要

应用程序将图像保持在分辨率中很重要,但欢迎使用任何编码技巧。我已经在互联网上搜索了几天,除了我已经实施的之外找不到任何东西。这里有关于这个主题的另一个线程,但似乎没有任何解决方案。希望你能帮忙。

    public Bitmap getBitmap(final Context c) {
    if (bitmap != null)
        return bitmap;

    final int rotate = necessaryRotation(c, file);
    // if(rotate != 0) rotateImageFile(c, rotate);

    try {
        // Get scaled version
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file, options);
        options.inSampleSize = calcInSampleSize(options, 1024, 1024);
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(file, options);

        // rotate?
        bitmap = rotateImage(c,bitmap,rotate);

        System.out.println("Bitmap loaded from file: size="
                + bitmap.getWidth() + "," + bitmap.getHeight());

        System.gc();
    } catch (Exception e) {
        System.err.println("Unable to load image file: "
                + this.getFilename());
    }

    // if rotation is needed, do it in worker thread for next time
    if(rotate != 0){
        Thread t = new Thread(new Runnable(){

            public void run() {
                // load entire image
                try{
                    File imageFile = new File(getFilename());
                    Bitmap huge = Media.getBitmap(c.getContentResolver(),
                    Uri.fromFile(imageFile));

                    huge = rotateImage(c,huge,rotate);

                    // save bitmap properly
                    FileOutputStream out = new FileOutputStream(imageFile);
                    huge.compress(Bitmap.CompressFormat.PNG, 100, out);

                    out.flush();
                    out.close();
                    huge.recycle();
                    huge = null;
                    out = null;
                    System.gc();

                }catch(IOException e){
                    e.printStackTrace();
                }
            }

        });
        t.start();
    }

    return bitmap;
}

private Bitmap rotateImage(Context c, Bitmap bitmap, int rotate) {
    if (rotate != 0) {
        // rotate
        Matrix m = new Matrix();
        m.postRotate(rotate);
        Bitmap rotImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), m, true);
        bitmap.recycle();

        System.out.println("Image (id=" + getId()
                + ") rotated successfully");

        System.gc();

        return rotImage;
    }
    return bitmap;
}

private int necessaryRotation(Context c, String imageFile) {
    int rotate = 0;
    ExifInterface exif;
    try {
        exif = new ExifInterface(imageFile);
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return rotate;
}

private int calcInSampleSize(BitmapFactory.Options options, int reqWidth,
        int reqHeight) {
    int height = options.outHeight;
    int width = options.outWidth;
    int inSampleSize = 1;
    while (height > reqHeight || width > reqWidth) {
        height /= 2;
        width /= 2;
        inSampleSize *= 2;
    }

    return inSampleSize;
}

如果您需要了解任何内容或进行任何优化,我可以用来减少内存使用量,请写信:) 谢谢

4

1 回答 1

0

试试这个片段:

private Bitmap rotateImage(Context c, Bitmap bitmap, int rotate) {
    ....

    // reduce byte per pixel
    bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);

    Bitmap.createBitmap(bitmap,...
}
于 2012-09-11T15:24:09.550 回答