List of usage examples for android.graphics Bitmap getWidth
public final int getWidth()
From source file:Main.java
public static boolean shouldScaleDownBitmap(Context context, Bitmap bitmap) { if (context != null && bitmap != null && bitmap.getWidth() > 0 && bitmap.getHeight() > 0) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics);//from w w w. j a v a 2 s.c o m int width = metrics.widthPixels; int height = metrics.heightPixels; return ((width != 0 && width / bitmap.getWidth() < 1) || (height != 0 && height / bitmap.getHeight() < 1)); } return false; }
From source file:Main.java
public static byte[] rgb2YUV(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); int len = width * height; byte[] yuv = new byte[len * 3 / 2]; int y, u, v;//from w ww . j a v a 2s.c o m for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int rgb = pixels[i * width + j] & 0x00FFFFFF; int r = rgb & 0xFF; int g = (rgb >> 8) & 0xFF; int b = (rgb >> 16) & 0xFF; y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16; u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128; v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128; y = y < 16 ? 16 : (y > 255 ? 255 : y); u = u < 0 ? 0 : (u > 255 ? 255 : u); v = v < 0 ? 0 : (v > 255 ? 255 : v); yuv[i * width + j] = (byte) y; // yuv[len + (i >> 1) * width + (j & ~1) + 0] = (byte) u; // yuv[len + (i >> 1) * width + (j & ~1) + 1] = (byte) v; } } return yuv; }
From source file:Main.java
public static Bitmap handleImageOldPhoto(Bitmap bm) { int width = bm.getWidth(); int height = bm.getHeight(); int color;/* w w w. j a va2 s . co m*/ int r, g, b, a; Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888); int[] oldPx = new int[width * height]; int[] newPx = new int[width * height]; bm.getPixels(oldPx, 0, width, 0, 0, width, height); for (int i = 0; i < width * height; i++) { color = oldPx[i]; r = Color.red(color); g = Color.green(color); b = Color.blue(color); a = Color.alpha(color); r = (int) (0.393 * r + 0.769 * g + 0.189 * b); g = (int) (0.349 * r + 0.686 * g + 0.168 * b); b = (int) (0.272 * r + 0.534 * g + 0.131 * b); if (r > 255) { r = 255; } if (g > 255) { g = 255; } if (b > 255) { b = 255; } newPx[i] = Color.argb(a, r, g, b); } bmp.setPixels(newPx, 0, width, 0, 0, width, height); return bmp; }
From source file:Main.java
/** * Method to create a fully-transparent Bitmap using the same size of the source passed as * parameter and also the same density.// ww w. j a v a2s . com * * @param source The original Bitmap. * @return A transparent Bitmap with the same size of the source. */ public static Bitmap clear(Bitmap source) { Bitmap newBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight()); //to erase the color from a Bitmap, I must use a mutable Bitmap!!!! Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true); mutableBitmap.eraseColor(Color.TRANSPARENT); return mutableBitmap; }
From source file:Main.java
public static PointF drawBitmapOffset(PointF center, Bitmap bitmap) { PointF offset = new PointF(); if (bitmap == null || bitmap.getWidth() < 0) { return center; }/* www. j a v a 2s . c om*/ offset.set(center.x - bitmap.getWidth() / 2, center.y - bitmap.getHeight() / 2); return offset; }
From source file:Main.java
public static Bitmap getCroppedBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); paint.setAntiAlias(true);/*www .ja v a 2 s. c o m*/ canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); // canvas.drawRoundRect(rectF, roundPx, roundPx, paint); canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); // return _bmp; return output; }
From source file:Main.java
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) width / w); float scaleHeight = ((float) height / h); matrix.postScale(scaleWidth, scaleHeight); Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); return newbmp; }
From source file:Main.java
public static Bitmap resizeOuterFit(Bitmap src, Rect rect) { float aspectRatio = Math.max((float) rect.width() / src.getWidth(), (float) rect.height() / src.getHeight()); int newWidth = (int) (src.getWidth() * aspectRatio); int newHeight = (int) (src.getHeight() * aspectRatio); return Bitmap.createScaledBitmap(src, newWidth, newHeight, false); }
From source file:Main.java
public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { final int w = bm.getWidth(); final int h = bm.getHeight(); final float sw = ((float) newWidth) / w; final float sh = ((float) newHeight) / h; Matrix matrix = new Matrix(); matrix.postScale(sw, sh);// www . j a va 2s .c o m return Bitmap.createBitmap(bm, 0, 0, w, h, matrix, false); }
From source file:Main.java
/** * Create a bitmap which is wrapped to circle * (similar to what you can see in G+ profile pic.) * * @param bitmap Original Bitmap//from www. ja v a 2s.co m * @return Circled bitmap */ public static Bitmap createCircleBitmap(Bitmap bitmap) { final int width = bitmap.getWidth(); final int height = bitmap.getHeight(); BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); Paint bitmapPaint = new Paint(); bitmapPaint.setAntiAlias(true); bitmapPaint.setShader(bitmapShader); Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2, bitmapPaint); return output; }