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

The following are Jave code examples for showing how to use getWidth() 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: TransformationUtils.java   View Source Code Vote up 6 votes
/**
 * Rotate and/or flip the image to match the given exif orientation.
 *
 * @param pool            A pool that may or may not contain an image of the necessary
 *                        dimensions.
 * @param inBitmap        The bitmap to rotate/flip.
 * @param exifOrientation the exif orientation [1-8].
 * @return The rotated and/or flipped image or toOrient if no rotation or flip was necessary.
 */
public static Bitmap rotateImageExif(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap,
    int exifOrientation) {
  if (!isExifOrientationRequired(exifOrientation)) {
    return inBitmap;
  }

  final Matrix matrix = new Matrix();
  initializeMatrixForRotation(exifOrientation, matrix);

  // From Bitmap.createBitmap.
  final RectF newRect = new RectF(0, 0, inBitmap.getWidth(), inBitmap.getHeight());
  matrix.mapRect(newRect);

  final int newWidth = Math.round(newRect.width());
  final int newHeight = Math.round(newRect.height());

  Bitmap.Config config = getSafeConfig(inBitmap);
  Bitmap result = pool.get(newWidth, newHeight, config);

  matrix.postTranslate(-newRect.left, -newRect.top);

  applyMatrix(inBitmap, result, matrix);
  return result;
}
 
Example 2
Project: react-native-sunmi-inner-printer   File: ESCUtil.java   View Source Code Vote up 6 votes
/**
 * 灰度图片黑白化,黑色是1,白色是0
 *
 * @param x   横坐标
 * @param y   纵坐标
 * @param bit 位图
 * @return
 */
public static byte px2Byte(int x, int y, Bitmap bit) {
    if (x < bit.getWidth() && y < bit.getHeight()) {
        byte b;
        int pixel = bit.getPixel(x, y);
        int red = (pixel & 0x00ff0000) >> 16; // 取高两位
        int green = (pixel & 0x0000ff00) >> 8; // 取中两位
        int blue = pixel & 0x000000ff; // 取低两位
        int gray = RGB2Gray(red, green, blue);
        if (gray < 128) {
            b = 1;
        } else {
            b = 0;
        }
        return b;
    }
    return 0;
}
 
Example 3
Project: Pocket-Plays-for-Twitch   File: RoundedTopTransformation.java   View Source Code Vote up 6 votes
@Override
public Bitmap transform(Bitmap source) {

    int w = source.getWidth();
    int h = source.getHeight();
    Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmp);
    Shader shader = new BitmapShader(source, Shader.TileMode.MIRROR,
            Shader.TileMode.MIRROR);

    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    paint.setAntiAlias(true);
    paint.setShader(shader);
    RectF rec = new RectF(0, 0, w, h - (h/3));
    c.drawRect(new RectF(0, (h/3), w, h), paint);
    c.drawRoundRect(rec, cornerRadius, cornerRadius, paint);
    source.recycle();
    return bmp;

}
 
Example 4
Project: Glide-transformations   File: RoundedCornersTransformation.java   View Source Code Vote up 6 votes
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();

  Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  }

  Canvas canvas = new Canvas(bitmap);
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
  drawRoundRect(canvas, paint, width, height);
  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 5
Project: Mvp-Retrofit-Rxjava-Rxbus   File: BitmapUtils.java   View Source Code Vote up 6 votes
/**
 * 图片圆角
 * 
 * @param bitmap
 * @param pixels
 * @return
 */
public static Bitmap getRoundBitmap(Bitmap bitmap, int pixels) {
	if (bitmap == null) {
		return null;
	}

	Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
			bitmap.getHeight(), Config.ARGB_8888);
	Canvas canvas = new Canvas(output);

	final int color = 0xffffffff;
	final Paint paint = new Paint();
	final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
	final RectF rectF = new RectF(rect);
	final float roundPx = pixels;

	paint.setAntiAlias(true);
	canvas.drawARGB(0, 0, 0, 0);
	paint.setColor(color);
	canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

	paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	canvas.drawBitmap(bitmap, rect, rect, paint);
	return output;
}
 
Example 6
Project: baselibrary-master   File: GlideRoundTransform.java   View Source Code Vote up 6 votes
private static Bitmap roundCrop(BitmapPool pool, Bitmap source)
{
    if (source == null) return null;

    Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    if (result == null)
    {
        result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
    paint.setAntiAlias(true);
    RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
    canvas.drawRoundRect(rectF, radius, radius, paint);
    return result;
}
 
Example 7
Project: ClouldReader   File: QRCodeUtil.java   View Source Code Vote up 5 votes
/**
 * 在二维码中间添加Logo图案
 */
private static Bitmap addLogo(Bitmap src, Bitmap logo) {
    if (src == null) {
        return null;
    }

    if (logo == null) {
        return src;
    }

    //获取图片的宽高
    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();
    int logoWidth = logo.getWidth();
    int logoHeight = logo.getHeight();

    if (srcWidth == 0 || srcHeight == 0) {
        return null;
    }

    if (logoWidth == 0 || logoHeight == 0) {
        return src;
    }

    //logo大小为二维码整体大小的1/5
    float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
    Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
    try {
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(src, 0, 0, null);
        canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
        canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);

        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
    } catch (Exception e) {
        bitmap = null;
        e.getStackTrace();
    }

    return bitmap;
}
 
Example 8
Project: GitHub   File: RoundedImageView.java   View Source Code Vote up 5 votes
private Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) {
    Bitmap finalBitmap;

    if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) {
        finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
    } else {
        finalBitmap = bitmap;
    }

    Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
            finalBitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
            finalBitmap.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(finalBitmap.getWidth() / 2,
            finalBitmap.getHeight() / 2,
            finalBitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(finalBitmap, rect, rect, paint);

    return output;
}
 
Example 9
Project: FlickLauncher   File: TiledImageRenderer.java   View Source Code Vote up 5 votes
boolean decode() {
    // Get a tile from the original image. The tile is down-scaled
    // by (1 << mTilelevel) from a region in the original image.
    try {
        Bitmap reuse = sTilePool.acquire();
        if (reuse != null && reuse.getWidth() != mTileSize) {
            reuse = null;
        }
        mDecodedTile = mModel.getTile(mTileLevel, mX, mY, reuse);
    } catch (Throwable t) {
        Log.w(TAG, "fail to decode tile", t);
    }
    return mDecodedTile != null;
}
 
Example 10
Project: QRScanner   File: BGAQRCodeUtil.java   View Source Code Vote up 5 votes
public static Bitmap adjustPhotoRotation(Bitmap inputBitmap, int orientationDegree) {
    if (inputBitmap == null) {
        return null;
    }

    Matrix matrix = new Matrix();
    matrix.setRotate(orientationDegree, (float) inputBitmap.getWidth() / 2, (float) inputBitmap.getHeight() / 2);
    float outputX, outputY;
    if (orientationDegree == 90) {
        outputX = inputBitmap.getHeight();
        outputY = 0;
    } else {
        outputX = inputBitmap.getHeight();
        outputY = inputBitmap.getWidth();
    }

    final float[] values = new float[9];
    matrix.getValues(values);
    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];
    matrix.postTranslate(outputX - x1, outputY - y1);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap.getHeight(), inputBitmap.getWidth(), Bitmap.Config.ARGB_8888);
    Paint paint = new Paint();
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawBitmap(inputBitmap, matrix, paint);
    return outputBitmap;
}
 
Example 11
Project: android-mvvm-architecture   File: RoundedImageView.java   View Source Code Vote up 5 votes
private Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) {
    Bitmap finalBitmap;

    if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) {
        finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
    } else {
        finalBitmap = bitmap;
    }

    Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
            finalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
            finalBitmap.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(finalBitmap.getWidth() / 2,
            finalBitmap.getHeight() / 2,
            finalBitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(finalBitmap, rect, rect, paint);

    return output;
}
 
Example 12
Project: BBSSDK-for-Android   File: ImageUtils.java   View Source Code Vote up 5 votes
/**
 * 转为圆角图片
 *
 * @param src         源图片
 * @param radius      圆角的度数
 * @param borderSize  边框尺寸
 * @param borderColor 边框颜色
 * @param recycle     是否回收
 * @return 圆角图片
 */
public static Bitmap toRoundCorner(final Bitmap src, final float radius, int borderSize, int borderColor, final boolean recycle) {
	if (isEmptyBitmap(src)) {
		return null;
	}
	int width = src.getWidth();
	int height = src.getHeight();
	Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	Bitmap ret = Bitmap.createBitmap(width, height, src.getConfig());
	BitmapShader shader = new BitmapShader(src, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
	paint.setShader(shader);
	Canvas canvas = new Canvas(ret);
	RectF rectF = new RectF(0, 0, width, height);
	float halfBorderSize = borderSize / 2f;
	rectF.inset(halfBorderSize, halfBorderSize);
	canvas.drawRoundRect(rectF, radius, radius, paint);
	if (borderSize > 0) {
		paint.setShader(null);
		paint.setColor(borderColor);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(borderSize);
		paint.setStrokeCap(Paint.Cap.ROUND);
		canvas.drawRoundRect(rectF, radius, radius, paint);
	}
	if (recycle && !src.isRecycled()) {
		src.recycle();
	}
	return ret;
}
 
Example 13
Project: AndroidDevSamples   File: BitmapUtil.java   View Source Code Vote up 5 votes
/**
 * Bitmap 宽高缩放
 */
public static Bitmap scaledBitmap(Bitmap source, float destWidth, float destHeight) {
    int srcWidth = source.getWidth();
    int srcHeight = source.getHeight();
    float scaleX = (float) destWidth / srcWidth;// 要强制转换
    float scaleY = (float) destHeight / srcHeight;
    float scale = Math.min(scaleX, scaleY);
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale); // 长和宽放大缩小的比例
    return Bitmap.createBitmap(source, 0, 0, srcWidth, srcHeight, matrix, true);
}
 
Example 14
Project: LEDView   File: LEDView.java   View Source Code Vote up 5 votes
private void initLEDFrame(LEDFrame ledFrame,Bitmap bitmap){
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Log.d("scott","bitmap w : " + w + "   bitmap h = " + h);
    for(int x = 0;x<w;x++){
        for(int y = 0;y<h;y++){
            ledFrame.fillData(y,x,bitmap.getPixel(x,y));
        }
    }
}
 
Example 15
Project: KTalk   File: EaseImageView.java   View Source Code Vote up 5 votes
/**
 * draw Rounded Rectangle
 *
 * @param canvas
 * @param bitmap
 */
private void drawDrawable(Canvas canvas, Bitmap bitmap) {
    Paint paint = new Paint();
    paint.setColor(0xffffffff);
    paint.setAntiAlias(true); //smooths out the edges of what is being drawn
    PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
    // set flags
    int saveFlags = Canvas.MATRIX_SAVE_FLAG
            | Canvas.CLIP_SAVE_FLAG
            | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
            | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
            | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
    canvas.saveLayer(0, 0, width, height, null, saveFlags);

    if (shapeType == 1) {
        canvas.drawCircle(width / 2, height / 2, width / 2 - 1, paint);
    } else if (shapeType == 2) {
        RectF rectf = new RectF(1, 1, getWidth() - 1, getHeight() - 1);
        canvas.drawRoundRect(rectf, radius + 1, radius + 1, paint);
    }

    paint.setXfermode(xfermode);

    float scaleWidth = ((float) getWidth()) / bitmap.getWidth();
    float scaleHeight = ((float) getHeight()) / bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    //bitmap scale
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

    canvas.drawBitmap(bitmap, 0, 0, paint);
    canvas.restore();
}
 
Example 16
Project: ESPD   File: SmartTexture.java   View Source Code Vote up 5 votes
public SmartTexture(Bitmap bitmap, int filtering, int wrapping, boolean premultiplied) {

		this.bitmap = bitmap;
		width = bitmap.getWidth();
		height = bitmap.getHeight();
		this.fModeMin = this.fModeMax = filtering;
		this.wModeH = this.wModeV = wrapping;
		this.premultiplied = premultiplied;

	}
 
Example 17
Project: SOS-The-Healthcare-Companion   File: ContactPictureLoader.java   View Source Code Vote up 4 votes
public static Bitmap retrievePicture(Context context, Uri photoUri, boolean roundContactPictures) {
    if (context == null || photoUri == null || Helper.isNullOrEmpty(photoUri.toString())) {
        return null;
    }

    InputStream stream = null;
    Bitmap bitmap = null;

    // read contact picture
    try {
        stream = context.getContentResolver().openInputStream(photoUri);
        bitmap = BitmapFactory.decodeStream(stream);

        if (bitmap != null) {
            // some contact pictures aren't square...
            if (bitmap.getWidth() != bitmap.getHeight()) {
                int w = bitmap.getWidth();
                int h = bitmap.getHeight();
                int indent = Math.abs(w - h) / 2;
                int x = w > h ? indent : 0;
                int y = w < h ? indent : 0;
                int size = Math.min(w, h);
                bitmap = createBitmap(bitmap, x, y, size, size);
            }

            if (roundContactPictures) {
                bitmap = getRoundedBitmap(bitmap);
            }
        }
    } catch (OutOfMemoryError | FileNotFoundException ignore) {
    } finally {
        Helper.closeQuietly(stream);
    }

    // cache contact picture
    if (bitmap != null) {
        ContactPictureCache.getInstance(context).put(photoUri, bitmap);
    }

    return bitmap;
}
 
Example 18
Project: wangyi-Lyric-Parser   File: RenderScriptBlur.java   View Source Code Vote up 4 votes
private boolean canReuseAllocation(Bitmap bitmap) {
    return bitmap.getHeight() == lastBitmapHeight && bitmap.getWidth() == lastBitmapWidth;
}
 
Example 19
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 20
Project: EasyAndroid   File: QuickIndexView.java   View Source Code Vote up 4 votes
@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);

    paint.setColor(textColor);

    int letterLength = getLetterLength();
    if(letterLength == 0)
    {
        return;
    }

    paint.setTextSize(letterLength);

    Paint.FontMetrics fontMetrics = paint.getFontMetrics();
    int y = getPaddingTop();

    int x = getWidth() >> 1;
    int halfLetterLength = letterLength >> 1;

    if(topDrawable != null)
    {
        Bitmap topBitmap = BitmapTool.toBitmap(topDrawable);
        bitmapRect.bottom = topBitmap.getHeight();
        bitmapRect.right = topBitmap.getWidth();
        outRect.left = x - halfLetterLength;
        outRect.right = x + halfLetterLength;
        outRect.top = y;
        outRect.bottom = y + letterLength;
        canvas.drawBitmap(topBitmap, bitmapRect, outRect, null);
        y += letterLength;
    }

    int fontY = (int) (halfLetterLength - fontMetrics.top / 2 - fontMetrics.bottom / 2);
    int size = letterList.size();
    for(int i = 0; i < size; i++)
    {
        canvas.drawText(letterList.get(i), x, y + fontY, paint);
        y += letterLength;
    }

    if(bottomDrawable != null)
    {
        Bitmap bottomBitmap = BitmapTool.toBitmap(bottomDrawable);
        bitmapRect.bottom = bottomBitmap.getHeight();
        bitmapRect.right = bottomBitmap.getWidth();
        outRect.left = x - halfLetterLength;
        outRect.right = x + halfLetterLength;
        outRect.top = y;
        outRect.bottom = y + letterLength;
        canvas.drawBitmap(bottomBitmap, bitmapRect, outRect, null);
    }
}