Beispiel #1
0
  public static Bitmap getBitmap(ContentResolver contentResolver, Uri uri) {
    InputStream in = null;
    try {
      in = contentResolver.openInputStream(uri);

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

      BitmapFactory.decodeStream(in, null, o);
      in.close();

      int scale = Util.scalePow2(o.outHeight, o.outWidth);
      BitmapFactory.Options o2 = new BitmapFactory.Options();
      o2.inSampleSize = scale;
      in = contentResolver.openInputStream(uri);
      Bitmap b = BitmapFactory.decodeStream(in, null, o2);
      in.close();

      return b;
    } catch (FileNotFoundException e) {
      Log.e(TAG, "file " + uri.toString() + " not found");
    } catch (IOException e) {
      Log.e(TAG, "file " + uri.toString() + " not found");
    }
    return null;
  }
Beispiel #2
0
  // Rotates the bitmap by the specified degree.
  // If a new bitmap is created, the original bitmap is recycled.
  public static Bitmap rotateAndCrop(Bitmap b, int degrees, Rect crop) {
    if (b == null) return b;
    Bitmap b2 = null;
    int scale = Util.scalePow2(b.getHeight(), b.getWidth());
    if (scale != 1 && crop != null) {
      crop.left *= scale;
      crop.right *= scale;
      crop.bottom *= scale;
      crop.top *= scale;
    }
    try {
      if (degrees != 0) {
        Matrix m = new Matrix();
        m.setRotate(degrees, 0, 0);
        RectF r_rot = new RectF(0, 0, b.getWidth(), b.getHeight());
        m.mapRect(r_rot);
        m.postTranslate(-r_rot.left, -r_rot.top);

        //				r_rot.set(0,0,b.getWidth(),b.getHeight());
        //				m.mapRect(r_rot);
        //				Log.d(TAG, "rotated bitmap = "+r_rot.toString());

        if (crop == null) b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
        else {
          Matrix minv = new Matrix();
          m.invert(minv);
          //      			minv.postScale(scale, scale);
          RectF r = new RectF();
          r.set(crop);
          minv.mapRect(r);
          Log.d(TAG, "crop = " + crop.toString());
          r.round(crop);
          Log.d(TAG, "bitmap " + b.getDensity() + " " + b.getWidth() + " x " + b.getHeight());
          Log.d(TAG, "inv rotated crop = " + crop.toString());
          b2 = Bitmap.createBitmap(b, crop.left, crop.top, crop.width(), crop.height(), m, true);
        }
      } else {
        if (crop != null) {
          Log.d(TAG, "crop = " + crop.toString());
          Log.d(TAG, "bitmap " + b.getDensity() + " " + b.getWidth() + " x " + b.getHeight());
          b2 = Bitmap.createBitmap(b, crop.left, crop.top, crop.width(), crop.height());
          //  				b2 = Bitmap.createBitmap(b, scale*crop.left, scale*crop.top,
          //  						scale*crop.width(), scale*crop.height());
        } else b2 = b;
      }
    } catch (OutOfMemoryError ex) {
      // We have no memory to rotate. Return the original bitmap.
      b2 = b;
    }
    Assert.assertNotNull(b2);
    if (b == b2) {
      return b;
    } else {
      Log.d(TAG, "b != b2, recycling b");
      b.recycle();
      return b2;
    }
  }