List of usage examples for android.graphics Bitmap getPixels
public void getPixels(@ColorInt int[] pixels, int offset, int stride, int x, int y, int width, int height)
From source file:Main.java
/** * Replaces colors in a bitmap that are not farther away from a specific color than a given * threshold./* w w w .ja v a2 s. c o m*/ * * @param srcBitmap The source bitmap to scan. * @param mutateSrc Indicates whether to mutate srcBitmap or to produce a new one. * @param keepCr The red color to keep * @param keepCg The green color to keep * @param keepCb The blue color to keep * @param replaceColor The color to replace mismatched colors with * @param distance The distance threshold. * @param simpleBG Whether the bitmap has a simple background * @return Bitmap with replaced colors */ private static Bitmap replaceColors(Bitmap srcBitmap, boolean mutateSrc, int keepCr, int keepCg, int keepCb, int replaceColor, int distance, boolean simpleBG) { int[] allpixels = new int[srcBitmap.getHeight() * srcBitmap.getWidth()]; srcBitmap.getPixels(allpixels, 0, srcBitmap.getWidth(), 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight()); int bgColor = replaceColor; int distanceSq = distance * distance; if (simpleBG) { bgColor = allpixels[0]; } for (int i = 0; i < allpixels.length; i++) { /* Avoid unnecessary math for obviously background color. This removes most of the math * for candy, HP and name bitmaps. */ if (allpixels[i] == bgColor) { allpixels[i] = replaceColor; continue; } int rDiff = keepCr - Color.red(allpixels[i]); int gDiff = keepCg - Color.green(allpixels[i]); int bDiff = keepCb - Color.blue(allpixels[i]); int dSq = rDiff * rDiff + gDiff * gDiff + bDiff * bDiff; if (dSq > distanceSq) { allpixels[i] = replaceColor; } } Bitmap dstBitmap; if (mutateSrc) { dstBitmap = srcBitmap; } else { dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig()); } dstBitmap.setPixels(allpixels, 0, srcBitmap.getWidth(), 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight()); return dstBitmap; }
From source file:Main.java
/** * Returns true if 20% of the image's top right corner is white, or 20% of the bottom * of the image is white.//from w w w.ja va 2s . co m */ public static boolean isBitmapWhiteAtTopOrBottom(Bitmap largeBitmap) { Trace.beginSection("isBitmapWhiteAtTopOrBottom"); try { final Bitmap smallBitmap = scaleBitmapDown(largeBitmap); final int[] rgbPixels = new int[smallBitmap.getWidth() * smallBitmap.getHeight()]; smallBitmap.getPixels(rgbPixels, 0, smallBitmap.getWidth(), 0, 0, smallBitmap.getWidth(), smallBitmap.getHeight()); // look at top right corner of the bitmap int whiteCount = 0; for (int y = 0; y < smallBitmap.getHeight() * HEIGHT_PERCENT_ANALYZED; y++) { for (int x = (int) (smallBitmap.getWidth() * (1 - THIRD)); x < smallBitmap.getWidth(); x++) { final int rgb = rgbPixels[y * smallBitmap.getWidth() + x]; if (isWhite(rgb)) { whiteCount++; } } } int totalPixels = (int) (smallBitmap.getHeight() * smallBitmap.getWidth() * THIRD * HEIGHT_PERCENT_ANALYZED); if (whiteCount / (float) totalPixels > PROPORTION_WHITE_CUTOFF) { return true; } // look at bottom portion of bitmap whiteCount = 0; for (int y = (int) (smallBitmap.getHeight() * (1 - HEIGHT_PERCENT_ANALYZED)); y < smallBitmap .getHeight(); y++) { for (int x = 0; x < smallBitmap.getWidth(); x++) { final int rgb = rgbPixels[y * smallBitmap.getWidth() + x]; if (isWhite(rgb)) { whiteCount++; } } } totalPixels = (int) (smallBitmap.getHeight() * smallBitmap.getWidth() * HEIGHT_PERCENT_ANALYZED); return whiteCount / (float) totalPixels > PROPORTION_WHITE_CUTOFF; } finally { Trace.endSection(); } }
From source file:Main.java
private static Bitmap trimTransparentRegions(Bitmap input, int alphaThreshold) { final int height = input.getHeight(); final int width = input.getWidth(); final int[] pixels = new int[height * width]; input.getPixels(pixels, 0, width, 0, 0, width, height); int boundTop = 0, boundBottom = 0, boundLeft = 0, boundRight = 0; int i = 0;//w w w. ja v a 2 s .c om topScan: for (i = 0; i < pixels.length; i++) { if (isTransparent(pixels[i], alphaThreshold) == false) { boundTop = i / width; break topScan; } } bottomScan: for (i = pixels.length - 1; i >= 0; i--) { if (isTransparent(pixels[i], alphaThreshold) == false) { boundBottom = (pixels.length - i) / width; break bottomScan; } } /* for each x, x <- 0, scan downwards until not transparent pixel is reached */ leftScan: for (i = 0; i < width; i++) { for (int j = i; j < pixels.length; j += width) { if (isTransparent(pixels[j], alphaThreshold) == false) { boundLeft = j % width; break leftScan; } } } /* for each x, x <- width, scan upwards until not transparent pixel is reached */ rightScan: for (i = pixels.length - 1; i >= 0; i--) { for (int j = i; j >= 0; j -= width) { if (isTransparent(pixels[j], alphaThreshold) == false) { boundRight = width - (j % width); break rightScan; } } } return Bitmap.createBitmap(input, boundLeft, boundTop, width - boundLeft - boundRight, height - boundTop - boundBottom); }
From source file:Main.java
/** * Creates a copy of the bitmap by replacing the color of every pixel * by tintColor while keeping the alpha value. * @param bitmap The original bitmap./* w w w. j av a 2s . c om*/ * @param tintColor The color to apply to every pixel. * @return A copy of the given bitmap with the tint color applied. */ public static Bitmap applyColor(Bitmap bitmap, int tintColor) { int r = Color.red(tintColor); int g = Color.green(tintColor); int b = Color.blue(tintColor); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); for (int i = 0; i < pixels.length; i++) { int color = pixels[i]; int alpha = Color.alpha(color); pixels[i] = Color.argb(alpha, r, g, b); } return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888); }
From source file:Main.java
public static Bitmap getHistogram(Bitmap bmpOriginal) { //Scale bmpOriginal to improve performance final int dstWidth = 200; int dstHeight = dstWidth * bmpOriginal.getHeight() / bmpOriginal.getWidth(); Bitmap bmpScaled = Bitmap.createScaledBitmap(bmpOriginal, dstWidth, dstHeight, false); int[] histogramValues = new int[256]; int[] pixels = new int[dstWidth]; int pxBrightness; for (int row = 0; row < dstHeight; row++) { bmpScaled.getPixels(pixels, 0, dstWidth, 0, row, dstWidth, 1); for (int col = 0; col < dstWidth; col++) { pxBrightness = rgbToGray(pixels[col]); histogramValues[pxBrightness]++; }/* w w w. j a v a 2 s. c om*/ } bmpScaled.recycle(); int histogramMax = max(histogramValues); Bitmap bmpHistogram = Bitmap.createBitmap(256, histogramMax, Config.ARGB_8888); Canvas canvas = new Canvas(bmpHistogram); Paint paint = new Paint(); paint.setColor(Color.CYAN); for (int i = 0; i < 256; i++) canvas.drawLine(i, histogramMax - histogramValues[i], i, histogramMax, paint); return bmpHistogram; }
From source file:Main.java
/** * Convert bitmap format to <code>Bitmap.Config.ARGB_8888</code>. * * @param img//from w w w. j a v a 2 s . c o m * JPEG bitmap to convert * @return {@link Bitmap} in ARGB8888 format */ public static Bitmap convertBitmapFormatToARGB888(Bitmap img) { int numPixels = img.getWidth() * img.getHeight(); int[] pixels = new int[numPixels]; // Get JPEG pixels. Each int is the color values for one pixel. img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight()); // Create a Bitmap of the appropriate format. Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.ARGB_8888); // Set RGB pixels. result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight()); return result; }
From source file:Main.java
/** * Creates a copy of the bitmap by replacing the color of every pixel * by accentColor while keeping the alpha value. * @param bitmap The original bitmap.//from www . j a v a 2s .com * @param accentColor The color to apply to every pixel. * @return A copy of the given bitmap with the accent color applied. */ public static Bitmap applyColor(Bitmap bitmap, int accentColor) { int r = Color.red(accentColor); int g = Color.green(accentColor); int b = Color.blue(accentColor); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); for (int i = 0; i < pixels.length; i++) { int color = pixels[i]; int alpha = Color.alpha(color); pixels[i] = Color.argb(alpha, r, g, b); } return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888); }
From source file:Main.java
public static Drawable createGradientColorAndCover(Context context, int coveredResID, int CoverColor) { //final Resources res=context.getResources(); final Resources res = context.getResources(); Bitmap bitmap = BitmapFactory.decodeResource(res, coveredResID); if (bitmap == null) { return null; }/* w ww . j a v a2s.c om*/ final int length = bitmap.getWidth(); final int height = bitmap.getHeight(); int[] shape = new int[length * height]; int[] colors = new int[length * height]; bitmap.getPixels(shape, 0, length, 0, 0, length, height); int color = CoverColor; for (int i = 0; i < length * height; i++) { float percent = ((float) i % length / length) * 0xff; int alpha = ((int) percent << 6 * 4); alpha &= shape[i] & 0xFF000000; colors[i] = (alpha) | (color & 0x00FFFFFF); } Bitmap newbitmap = Bitmap.createBitmap(length, height, Bitmap.Config.ARGB_8888); newbitmap.setPixels(colors, 0, length, 0, 0, length, height); Bitmap fooBitmap = Bitmap.createBitmap(length, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(fooBitmap); Paint paint = new Paint(); canvas.drawBitmap(bitmap, 0, 0, paint); canvas.drawBitmap(newbitmap, 0, 0, paint); newbitmap.recycle(); bitmap.recycle(); return new BitmapDrawable(res, fooBitmap); }
From source file:Main.java
/** * Create a bitmap that contains the transformation to make to create the final * bitmap with a new tint color. You can then call processTintTransformationMap() * to get a bitmap with the desired tint. * @param bitmap The original bitmap./*w ww . j a va2 s. c o m*/ * @param tintColor Tint color in the original bitmap. * @return A transformation map to be used with processTintTransformationMap(). The * transformation values are stored in the red and green values. The alpha value is * significant and the blue value can be ignored. */ public static Bitmap createTintTransformationMap(Bitmap bitmap, int tintColor) { // tint color int[] t = new int[] { Color.red(tintColor), Color.green(tintColor), Color.blue(tintColor) }; int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); int maxIndex = getMaxIndex(t); int mintIndex = getMinIndex(t); for (int i = 0; i < pixels.length; i++) { int color = pixels[i]; // pixel color int[] p = new int[] { Color.red(color), Color.green(color), Color.blue(color) }; int alpha = Color.alpha(color); float[] transformation = calculateTransformation(t[maxIndex], t[mintIndex], p[maxIndex], p[mintIndex]); pixels[i] = Color.argb(alpha, (int) (transformation[0] * 255), (int) (transformation[1] * 255), 0); } return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888); }
From source file:Main.java
public static Bitmap handleImageOldPhoto(Bitmap bm) { int width = bm.getWidth(); int height = bm.getHeight(); int color;/* ww w .j a v a2 s. c om*/ 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; }