Java Code Examples for android.graphics.Bitmap.equals()

The following are Jave code examples for showing how to use equals() of the android.graphics.Bitmap class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: GitHub   File: BitmapTransformation.java   View Source Code Vote up 6 votes
@Override
public final Resource<Bitmap> transform(
    Context context, Resource<Bitmap> resource, int outWidth, int outHeight) {
  if (!Util.isValidDimensions(outWidth, outHeight)) {
    throw new IllegalArgumentException(
        "Cannot apply transformation on width: " + outWidth + " or height: " + outHeight
            + " less than or equal to zero and not Target.SIZE_ORIGINAL");
  }
  BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
  Bitmap toTransform = resource.get();
  int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth;
  int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight;
  Bitmap transformed = transform(bitmapPool, toTransform, targetWidth, targetHeight);

  final Resource<Bitmap> result;
  if (toTransform.equals(transformed)) {
    result = resource;
  } else {
    result = BitmapResource.obtain(transformed, bitmapPool);
  }
  return result;
}
 
Example 2
Project: GitHub   File: BitmapTransformation.java   View Source Code Vote up 6 votes
@Override
public final Resource<Bitmap> transform(
    Context context, Resource<Bitmap> resource, int outWidth, int outHeight) {
  if (!Util.isValidDimensions(outWidth, outHeight)) {
    throw new IllegalArgumentException(
        "Cannot apply transformation on width: " + outWidth + " or height: " + outHeight
            + " less than or equal to zero and not Target.SIZE_ORIGINAL");
  }
  BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
  Bitmap toTransform = resource.get();
  int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth;
  int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight;
  Bitmap transformed = transform(bitmapPool, toTransform, targetWidth, targetHeight);

  final Resource<Bitmap> result;
  if (toTransform.equals(transformed)) {
    result = resource;
  } else {
    result = BitmapResource.obtain(transformed, bitmapPool);
  }
  return result;
}
 
Example 3
Project: boohee_v5.6   File: RoundedTransformationBuilder.java   View Source Code Vote up 6 votes
public Transformation build() {
    return new Transformation() {
        public Bitmap transform(Bitmap source) {
            Bitmap transformed = RoundedDrawable.fromBitmap(source).setScaleType
                    (RoundedTransformationBuilder.this.mScaleType).setCornerRadius
                    (RoundedTransformationBuilder.this.mCornerRadii[0],
                            RoundedTransformationBuilder.this.mCornerRadii[1],
                            RoundedTransformationBuilder.this.mCornerRadii[2],
                            RoundedTransformationBuilder.this.mCornerRadii[3]).setBorderWidth
                    (RoundedTransformationBuilder.this.mBorderWidth).setBorderColor
                    (RoundedTransformationBuilder.this.mBorderColor).setOval
                    (RoundedTransformationBuilder.this.mOval).toBitmap();
            if (!source.equals(transformed)) {
                source.recycle();
            }
            return transformed;
        }

        public String key() {
            return "r:" + Arrays.toString(RoundedTransformationBuilder.this.mCornerRadii) +
                    "b:" + RoundedTransformationBuilder.this.mBorderWidth + "c:" +
                    RoundedTransformationBuilder.this.mBorderColor + "o:" + RoundedTransformationBuilder.this.mOval;
        }
    };
}
 
Example 4
Project: shareNote   File: RoundedTransformationBuilder.java   View Source Code Vote up 6 votes
/**
 * Creates a {@link Transformation} for use with picasso.
 *
 * @return the {@link Transformation}
 */
public Transformation build() {
  return new Transformation() {
    @Override public Bitmap transform(Bitmap source) {
      Bitmap transformed = RoundedDrawable.fromBitmap(source)
          .setScaleType(mScaleType)
          .setCornerRadius(mCornerRadii[0], mCornerRadii[1], mCornerRadii[2], mCornerRadii[3])
          .setBorderWidth(mBorderWidth)
          .setBorderColor(mBorderColor)
          .setOval(mOval)
          .toBitmap();
      if (!source.equals(transformed)) {
        source.recycle();
      }
      return transformed;
    }

    @Override public String key() {
      return "r:" + Arrays.toString(mCornerRadii)
          + "b:" + mBorderWidth
          + "c:" + mBorderColor
          + "o:" + mOval;
    }
  };
}
 
Example 5
Project: Ocr-android   File: BitmapUtils.java   View Source Code Vote up 6 votes
/**
 * 旋转bitmap
 *
 * @param origin bitmap
 * @param alpha  旋转角度
 * @return 旋转后的bitmap
 */
public static Bitmap rotateBitmap(Bitmap origin, float alpha) {
    if (origin == null || origin.isRecycled()) {
        return null;
    }
    int width = origin.getWidth();
    int height = origin.getHeight();
    Matrix matrix = new Matrix();
    matrix.setRotate(alpha);
    Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
    if (newBM.equals(origin)) {
        return newBM;
    }
    origin.recycle();
    return newBM;
}
 
Example 6
Project: GitHub   File: TransformationUtils.java   View Source Code Vote up 5 votes
/**
 * Creates a bitmap from a source bitmap and rounds the corners.
 *
 * @param inBitmap the source bitmap to use as a basis for the created bitmap.
 * @param width the width of the generated bitmap.
 * @param height the height of the generated bitmap.
 * @param roundingRadius the corner radius to be applied (in device-specific pixels).
 * @return a {@link Bitmap} similar to inBitmap but with rounded corners.
 * @throws IllegalArgumentException if roundingRadius, width or height is 0 or less.
 */
public static Bitmap roundedCorners(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap,
    int width, int height, int roundingRadius) {
  Preconditions.checkArgument(width > 0, "width must be greater than 0.");
  Preconditions.checkArgument(height > 0, "height must be greater than 0.");
  Preconditions.checkArgument(roundingRadius > 0, "roundingRadius must be greater than 0.");

  // Alpha is required for this transformation.
  Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);
  Bitmap result = pool.get(width, height, Bitmap.Config.ARGB_8888);

  result.setHasAlpha(true);

  BitmapShader shader = new BitmapShader(toTransform, Shader.TileMode.CLAMP,
      Shader.TileMode.CLAMP);
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  paint.setShader(shader);
  RectF rect = new RectF(0, 0, result.getWidth(), result.getHeight());
  BITMAP_DRAWABLE_LOCK.lock();
  try {
    Canvas canvas = new Canvas(result);
    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    canvas.drawRoundRect(rect, roundingRadius, roundingRadius, paint);
    clear(canvas);
  } finally {
    BITMAP_DRAWABLE_LOCK.unlock();
  }

  if (!toTransform.equals(inBitmap)) {
    pool.put(toTransform);
  }

  return result;
}
 
Example 7
Project: GitHub   File: TransformationUtils.java   View Source Code Vote up 5 votes
/**
 * Creates a bitmap from a source bitmap and rounds the corners.
 *
 * <p>This method does <em>NOT</em> resize the given {@link Bitmap}, it only rounds it's corners.
 * To both resize and round the corners of an image, consider
 * {@link com.bumptech.glide.request.RequestOptions#transforms(Transformation[])} and/or
 * {@link com.bumptech.glide.load.MultiTransformation}.
 *
 * @param inBitmap the source bitmap to use as a basis for the created bitmap.
 * @param roundingRadius the corner radius to be applied (in device-specific pixels).
 * @return a {@link Bitmap} similar to inBitmap but with rounded corners.
 * @throws IllegalArgumentException if roundingRadius, width or height is 0 or less.
 */
public static Bitmap roundedCorners(
    @NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int roundingRadius) {
  Preconditions.checkArgument(roundingRadius > 0, "roundingRadius must be greater than 0.");

  // Alpha is required for this transformation.
  Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);
  Bitmap result =
      pool.get(toTransform.getWidth(), toTransform.getHeight(), Bitmap.Config.ARGB_8888);

  result.setHasAlpha(true);

  BitmapShader shader = new BitmapShader(toTransform, Shader.TileMode.CLAMP,
      Shader.TileMode.CLAMP);
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  paint.setShader(shader);
  RectF rect = new RectF(0, 0, result.getWidth(), result.getHeight());
  BITMAP_DRAWABLE_LOCK.lock();
  try {
    Canvas canvas = new Canvas(result);
    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    canvas.drawRoundRect(rect, roundingRadius, roundingRadius, paint);
    clear(canvas);
  } finally {
    BITMAP_DRAWABLE_LOCK.unlock();
  }

  if (!toTransform.equals(inBitmap)) {
    pool.put(toTransform);
  }

  return result;
}
 
Example 8
Project: MyCreditCardDemo   File: CmbChinaLoginActivity.java   View Source Code Vote up 5 votes
private Bitmap scaleBitmap(Bitmap origin, float ratio) {
    if (origin == null) {
        return null;
    }
    int width = origin.getWidth();
    int height = origin.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(ratio, ratio);
    Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
    if (newBM.equals(origin)) {
        return newBM;
    }
    origin.recycle();
    return newBM;
}
 
Example 9
Project: CameraCardCropDemo   File: BitmapUtils.java   View Source Code Vote up 5 votes
private static Bitmap rotateBitmap(Bitmap origin, float alpha) {
    if (origin == null) {
        return null;
    }
    int width = origin.getWidth();
    int height = origin.getHeight();
    Matrix matrix = new Matrix();
    matrix.setRotate(alpha);
    Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
    if (newBM.equals(origin)) {
        return newBM;
    }
    origin.recycle();
    return newBM;
}
 
Example 10
Project: GitHub   File: TransformationUtils.java   View Source Code Vote up 4 votes
/**
 * Crop the image to a circle and resize to the specified width/height.  The circle crop will
 * have the same width and height equal to the min-edge of the result image.
 *
 * @param pool   The BitmapPool obtain a bitmap from.
 * @param inBitmap   The Bitmap to resize.
 * @param destWidth    The width in pixels of the final Bitmap.
 * @param destHeight   The height in pixels of the final Bitmap.
 * @return The resized Bitmap (will be recycled if recycled is not null).
 */
public static Bitmap circleCrop(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap,
    int destWidth, int destHeight) {
  int destMinEdge = Math.min(destWidth, destHeight);
  float radius = destMinEdge / 2f;

  int srcWidth = inBitmap.getWidth();
  int srcHeight = inBitmap.getHeight();

  float scaleX = destMinEdge / (float) srcWidth;
  float scaleY = destMinEdge / (float) srcHeight;
  float maxScale = Math.max(scaleX, scaleY);

  float scaledWidth = maxScale * srcWidth;
  float scaledHeight = maxScale * srcHeight;
  float left = (destMinEdge - scaledWidth) / 2f;
  float top = (destMinEdge - scaledHeight) / 2f;

  RectF destRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

  // Alpha is required for this transformation.
  Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);

  Bitmap result = pool.get(destMinEdge, destMinEdge, Bitmap.Config.ARGB_8888);
  result.setHasAlpha(true);

  BITMAP_DRAWABLE_LOCK.lock();
  try {
    Canvas canvas = new Canvas(result);
    // Draw a circle
    canvas.drawCircle(radius, radius, radius, CIRCLE_CROP_SHAPE_PAINT);
    // Draw the bitmap in the circle
    canvas.drawBitmap(toTransform, null, destRect, CIRCLE_CROP_BITMAP_PAINT);
    clear(canvas);
  } finally {
    BITMAP_DRAWABLE_LOCK.unlock();
  }

  if (!toTransform.equals(inBitmap)) {
    pool.put(toTransform);
  }

  return result;
}
 
Example 11
Project: GitHub   File: Downsampler.java   View Source Code Vote up 4 votes
private Bitmap decodeFromWrappedStreams(InputStream is,
    BitmapFactory.Options options, DownsampleStrategy downsampleStrategy,
    DecodeFormat decodeFormat, int requestedWidth, int requestedHeight,
    boolean fixBitmapToRequestedDimensions, DecodeCallbacks callbacks) throws IOException {

  int[] sourceDimensions = getDimensions(is, options, callbacks);
  int sourceWidth = sourceDimensions[0];
  int sourceHeight = sourceDimensions[1];
  String sourceMimeType = options.outMimeType;

  int orientation = ImageHeaderParserUtils.getOrientation(parsers, is, byteArrayPool);
  int degreesToRotate = TransformationUtils.getExifOrientationDegrees(orientation);

  options.inPreferredConfig = getConfig(is, decodeFormat);
  if (options.inPreferredConfig != Bitmap.Config.ARGB_8888) {
    options.inDither = true;
  }

  int targetWidth = requestedWidth == Target.SIZE_ORIGINAL ? sourceWidth : requestedWidth;
  int targetHeight = requestedHeight == Target.SIZE_ORIGINAL ? sourceHeight : requestedHeight;

  calculateScaling(downsampleStrategy, degreesToRotate, sourceWidth, sourceHeight, targetWidth,
      targetHeight, options);

  boolean isKitKatOrGreater = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  // Prior to KitKat, the inBitmap size must exactly match the size of the bitmap we're decoding.
  if ((options.inSampleSize == 1 || isKitKatOrGreater)
      && shouldUsePool(is)) {
    int expectedWidth;
    int expectedHeight;
    if (fixBitmapToRequestedDimensions && isKitKatOrGreater) {
      expectedWidth = targetWidth;
      expectedHeight = targetHeight;
    } else {
      float densityMultiplier = isScaling(options)
          ? (float) options.inTargetDensity / options.inDensity : 1f;
      int sampleSize = options.inSampleSize;
      int downsampledWidth = (int) Math.ceil(sourceWidth / (float) sampleSize);
      int downsampledHeight = (int) Math.ceil(sourceHeight / (float) sampleSize);
      expectedWidth = Math.round(downsampledWidth * densityMultiplier);
      expectedHeight = Math.round(downsampledHeight * densityMultiplier);

      if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "Calculated target [" + expectedWidth + "x" + expectedHeight + "] for source"
            + " [" + sourceWidth + "x" + sourceHeight + "]"
            + ", sampleSize: " + sampleSize
            + ", targetDensity: " + options.inTargetDensity
            + ", density: " + options.inDensity
            + ", density multiplier: " + densityMultiplier);
      }
    }
    // If this isn't an image, or BitmapFactory was unable to parse the size, width and height
    // will be -1 here.
    if (expectedWidth > 0 && expectedHeight > 0) {
      setInBitmap(options, bitmapPool, expectedWidth, expectedHeight);
    }
  }
  Bitmap downsampled = decodeStream(is, options, callbacks);
  callbacks.onDecodeComplete(bitmapPool, downsampled);

  if (Log.isLoggable(TAG, Log.VERBOSE)) {
    logDecode(sourceWidth, sourceHeight, sourceMimeType, options, downsampled,
        requestedWidth, requestedHeight);
  }

  Bitmap rotated = null;
  if (downsampled != null) {
    // If we scaled, the Bitmap density will be our inTargetDensity. Here we correct it back to
    // the expected density dpi.
    downsampled.setDensity(displayMetrics.densityDpi);

    rotated = TransformationUtils.rotateImageExif(bitmapPool, downsampled, orientation);
    if (!downsampled.equals(rotated)) {
      bitmapPool.put(downsampled);
    }
  }

  return rotated;
}
 
Example 12
Project: GitHub   File: TransformationUtils.java   View Source Code Vote up 4 votes
/**
 * Crop the image to a circle and resize to the specified width/height.  The circle crop will
 * have the same width and height equal to the min-edge of the result image.
 *
 * @param pool   The BitmapPool obtain a bitmap from.
 * @param inBitmap   The Bitmap to resize.
 * @param destWidth    The width in pixels of the final Bitmap.
 * @param destHeight   The height in pixels of the final Bitmap.
 * @return The resized Bitmap (will be recycled if recycled is not null).
 */
public static Bitmap circleCrop(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap,
    int destWidth, int destHeight) {
  int destMinEdge = Math.min(destWidth, destHeight);
  float radius = destMinEdge / 2f;

  int srcWidth = inBitmap.getWidth();
  int srcHeight = inBitmap.getHeight();

  float scaleX = destMinEdge / (float) srcWidth;
  float scaleY = destMinEdge / (float) srcHeight;
  float maxScale = Math.max(scaleX, scaleY);

  float scaledWidth = maxScale * srcWidth;
  float scaledHeight = maxScale * srcHeight;
  float left = (destMinEdge - scaledWidth) / 2f;
  float top = (destMinEdge - scaledHeight) / 2f;

  RectF destRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

  // Alpha is required for this transformation.
  Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);

  Bitmap result = pool.get(destMinEdge, destMinEdge, Bitmap.Config.ARGB_8888);
  result.setHasAlpha(true);

  BITMAP_DRAWABLE_LOCK.lock();
  try {
    Canvas canvas = new Canvas(result);
    // Draw a circle
    canvas.drawCircle(radius, radius, radius, CIRCLE_CROP_SHAPE_PAINT);
    // Draw the bitmap in the circle
    canvas.drawBitmap(toTransform, null, destRect, CIRCLE_CROP_BITMAP_PAINT);
    clear(canvas);
  } finally {
    BITMAP_DRAWABLE_LOCK.unlock();
  }

  if (!toTransform.equals(inBitmap)) {
    pool.put(toTransform);
  }

  return result;
}
 
Example 13
Project: jackknife   File: ShadeView.java   View Source Code Vote up 4 votes
public void setIconBitmap(Bitmap bitmap) {
    if (!bitmap.equals(mIconBitmap)) {
        mIconBitmap = bitmap;
        invalidateView();
    }
}