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:com.truebanana.bitmap.BitmapUtils.java
@SuppressLint("NewApi") public static Bitmap blur(Context context, Bitmap bitmap, float scale, int radius) { if (SystemUtils.getSDKVersion() >= Build.VERSION_CODES.JELLY_BEAN_MR1) { int width = Math.round(bitmap.getWidth() * scale); int height = Math.round(bitmap.getHeight() * scale); bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false); Bitmap processedBitmap = Bitmap.createBitmap(bitmap); RenderScript rs = RenderScript.create(context); ScriptIntrinsicBlur sib = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); Allocation tmpIn = Allocation.createFromBitmap(rs, bitmap); Allocation tmpOut = Allocation.createFromBitmap(rs, processedBitmap); sib.setRadius(radius);// w ww .j a v a 2 s . c om sib.setInput(tmpIn); sib.forEach(tmpOut); tmpOut.copyTo(processedBitmap); return processedBitmap; } else { int width = Math.round(bitmap.getWidth() * scale); int height = Math.round(bitmap.getHeight() * scale); bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false); Bitmap processedBitmap = bitmap.copy(bitmap.getConfig(), true); if (radius < 1) { return null; } int w = processedBitmap.getWidth(); int h = processedBitmap.getHeight(); int[] pix = new int[w * h]; processedBitmap.getPixels(pix, 0, w, 0, 0, w, h); int wm = w - 1; int hm = h - 1; int wh = w * h; int div = radius + radius + 1; int r[] = new int[wh]; int g[] = new int[wh]; int b[] = new int[wh]; int rsum, gsum, bsum, x, y, i, p, yp, yi, yw; int vmin[] = new int[Math.max(w, h)]; int divsum = (div + 1) >> 1; divsum *= divsum; int dv[] = new int[256 * divsum]; for (i = 0; i < 256 * divsum; i++) { dv[i] = (i / divsum); } yw = yi = 0; int[][] stack = new int[div][3]; int stackpointer; int stackstart; int[] sir; int rbs; int r1 = radius + 1; int routsum, goutsum, boutsum; int rinsum, ginsum, binsum; for (y = 0; y < h; y++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; for (i = -radius; i <= radius; i++) { p = pix[yi + Math.min(wm, Math.max(i, 0))]; sir = stack[i + radius]; sir[0] = (p & 0xff0000) >> 16; sir[1] = (p & 0x00ff00) >> 8; sir[2] = (p & 0x0000ff); rbs = r1 - Math.abs(i); rsum += sir[0] * rbs; gsum += sir[1] * rbs; bsum += sir[2] * rbs; if (i > 0) { rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; } else { routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; } } stackpointer = radius; for (x = 0; x < w; x++) { r[yi] = dv[rsum]; g[yi] = dv[gsum]; b[yi] = dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - radius + div; sir = stack[stackstart % div]; routsum -= sir[0]; goutsum -= sir[1]; boutsum -= sir[2]; if (y == 0) { vmin[x] = Math.min(x + radius + 1, wm); } p = pix[yw + vmin[x]]; sir[0] = (p & 0xff0000) >> 16; sir[1] = (p & 0x00ff00) >> 8; sir[2] = (p & 0x0000ff); rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1) % div; sir = stack[(stackpointer) % div]; routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; rinsum -= sir[0]; ginsum -= sir[1]; binsum -= sir[2]; yi++; } yw += w; } for (x = 0; x < w; x++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; yp = -radius * w; for (i = -radius; i <= radius; i++) { yi = Math.max(0, yp) + x; sir = stack[i + radius]; sir[0] = r[yi]; sir[1] = g[yi]; sir[2] = b[yi]; rbs = r1 - Math.abs(i); rsum += r[yi] * rbs; gsum += g[yi] * rbs; bsum += b[yi] * rbs; if (i > 0) { rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; } else { routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; } if (i < hm) { yp += w; } } yi = x; stackpointer = radius; for (y = 0; y < h; y++) { // Preserve alpha channel: ( 0xff000000 & pix[yi] ) pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - radius + div; sir = stack[stackstart % div]; routsum -= sir[0]; goutsum -= sir[1]; boutsum -= sir[2]; if (x == 0) { vmin[y] = Math.min(y + r1, hm) * w; } p = x + vmin[y]; sir[0] = r[p]; sir[1] = g[p]; sir[2] = b[p]; rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1) % div; sir = stack[stackpointer]; routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; rinsum -= sir[0]; ginsum -= sir[1]; binsum -= sir[2]; yi += w; } } processedBitmap.setPixels(pix, 0, w, 0, 0, w, h); return processedBitmap; } }
From source file:it.baywaylabs.jumpersumo.MainActivity.java
public void zxing(Mat mRgba) throws ChecksumException, FormatException { Bitmap bMap = Bitmap.createBitmap(mRgba.width(), mRgba.height(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(mRgba, bMap);// w ww .j a v a2 s. co m int[] intArray = new int[bMap.getWidth() * bMap.getHeight()]; //copy pixel data from the Bitmap into the 'intArray' array bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight()); LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Reader reader = new QRCodeMultiReader(); String sResult = ""; Double AREA_RIFERIMENTO = 11500.0; try { Result result = reader.decode(bitmap); sResult = result.getText(); if (result.getBarcodeFormat().compareTo(BarcodeFormat.QR_CODE) == 0) Log.d(TAG, "SI! E' Un QRCode"); ResultPoint[] points = result.getResultPoints(); Log.d(TAG, "PUNTI: " + points.toString()); //for (ResultPoint point : result.getResultPoints()) { Point a = new Point(points[0].getX(), points[0].getY()); Point b = new Point(points[2].getX(), points[2].getY()); Rect rect = new Rect(a, b); Log.d(TAG, "Area del rettangolo: " + rect.area()); if (rect.area() < AREA_RIFERIMENTO) Log.w(TAG, "Mi devo avvicinare!"); else Log.w(TAG, "Mi devo allontanare!"); Imgproc.rectangle(this.mRgba, new Point(points[0].getX(), points[0].getY()), new Point(points[2].getX(), points[2].getY()), new Scalar(0, 255, 0), 3); Log.d(TAG, sResult); Point center = new Point(0, 0); Imgproc.circle(this.mRgba, center, 10, new Scalar(0, 0, 255), 2); //if (!"".equals(sResult)) //Toast.makeText(MainActivity.this, "QRCode Scanned: " + sResult, Toast.LENGTH_LONG).show(); } catch (Resources.NotFoundException e) { Log.e(TAG, "Code Not Found"); e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } }
From source file:com.simadanesh.isatis.ScreenSlideActivity.java
private static byte[] StartBmpToPrintCode(Bitmap bitmap, int t) { byte temp = 0; int j = 7;//from w w w. j a va 2s.c o m int start = 0; if (bitmap != null) { int mWidth = bitmap.getWidth(); int mHeight = bitmap.getHeight(); int[] mIntArray = new int[mWidth * mHeight]; byte[] data = new byte[mWidth * mHeight]; bitmap.getPixels(mIntArray, 0, mWidth, 0, 0, mWidth, mHeight); encodeYUV420SP(data, mIntArray, mWidth, mHeight, t); byte[] result = new byte[mWidth * mHeight / 8]; for (int i = 0; i < mWidth * mHeight; i++) { temp = (byte) ((byte) (data[i] << j) + temp); j--; if (j < 0) { j = 7; } if (i % 8 == 7) { result[start++] = temp; temp = 0; } } if (j != 7) { result[start++] = temp; } int aHeight = 24 - mHeight % 24; byte[] add = new byte[aHeight * 48]; byte[] nresult = new byte[mWidth * mHeight / 8 + aHeight * 48]; System.arraycopy(result, 0, nresult, 0, result.length); System.arraycopy(add, 0, nresult, result.length, add.length); byte[] byteContent = new byte[(mWidth / 8 + 4) * (mHeight + aHeight)];// byte[] bytehead = new byte[4];// ??? bytehead[0] = (byte) 0x1f; bytehead[1] = (byte) 0x10; bytehead[2] = (byte) (mWidth / 8); bytehead[3] = (byte) 0x00; for (int index = 0; index < mHeight + aHeight; index++) { System.arraycopy(bytehead, 0, byteContent, index * 52, 4); System.arraycopy(nresult, index * 48, byteContent, index * 52 + 4, 48); } return byteContent; } return null; }
From source file:io.jawg.osmcontributor.ui.utils.BitmapHandler.java
/** * Get the white icon corresponding to a poiType. * * @param poiType the PoiType or null for notes. * @return The white icon.//from w w w .ja va2s . c o m */ public Drawable getIconWhite(PoiType poiType) { Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), poiType == null ? R.drawable.open_book : getIconDrawableId(poiType)); myBitmap = myBitmap.copy(myBitmap.getConfig(), true); int[] allpixels = new int[myBitmap.getHeight() * myBitmap.getWidth()]; myBitmap.getPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight()); for (int i = 0; i < myBitmap.getHeight() * myBitmap.getWidth(); i++) { if (allpixels[i] != 0) { int A = Color.alpha(allpixels[i]); // inverting byte for each R/G/B channel int R = 255 - Color.red(allpixels[i]); int G = 255 - Color.green(allpixels[i]); int B = 255 - Color.blue(allpixels[i]); // set newly-inverted pixel to output image allpixels[i] = Color.argb(A, R, G, B); } } myBitmap.setPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight()); return new BitmapDrawable(context.getResources(), myBitmap); }
From source file:Main.java
public static void fastblur(Bitmap srcBitmap, Bitmap destBitmap, int w, int h, int radius) { // Stack Blur v1.0 from // http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html ////from ww w.j a va2 s.com // Java Author: Mario Klingemann <mario at quasimondo.com> // http://incubator.quasimondo.com // created Feburary 29, 2004 // Android port : Yahel Bouaziz <yahel at kayenko.com> // http://www.kayenko.com // ported april 5th, 2012 // This is a compromise between Gaussian Blur and Box blur // It creates much better looking blurs than Box Blur, but is // 7x faster than my Gaussian Blur implementation. // // I called it Stack Blur because this describes best how this // filter works internally: it creates a kind of moving stack // of colors whilst scanning through the image. Thereby it // just has to add one new block of color to the right side // of the stack and remove the leftmost color. The remaining // colors on the topmost layer of the stack are either added on // or reduced by one, depending on if they are on the right or // on the left side of the stack. // // If you are using this algorithm in your code please add // the following line: // // Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com> if (radius < 1) { return; } int[] pix = new int[w * h]; srcBitmap.getPixels(pix, 0, w, 0, 0, w, h); int wm = w - 1; int hm = h - 1; int wh = w * h; int div = radius + radius + 1; int r[] = new int[wh]; int g[] = new int[wh]; int b[] = new int[wh]; int rsum, gsum, bsum, x, y, i, p, yp, yi, yw; int vmin[] = new int[Math.max(w, h)]; int divsum = (div + 1) >> 1; divsum *= divsum; int dv[] = new int[256 * divsum]; for (i = 0; i < 256 * divsum; i++) { dv[i] = (i / divsum); } yw = yi = 0; int[][] stack = new int[div][3]; int stackpointer; int stackstart; int[] sir; int rbs; int r1 = radius + 1; int routsum, goutsum, boutsum; int rinsum, ginsum, binsum; for (y = 0; y < h; y++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; for (i = -radius; i <= radius; i++) { p = pix[yi + Math.min(wm, Math.max(i, 0))]; sir = stack[i + radius]; sir[0] = (p & 0xff0000) >> 16; sir[1] = (p & 0x00ff00) >> 8; sir[2] = (p & 0x0000ff); rbs = r1 - Math.abs(i); rsum += sir[0] * rbs; gsum += sir[1] * rbs; bsum += sir[2] * rbs; if (i > 0) { rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; } else { routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; } } stackpointer = radius; for (x = 0; x < w; x++) { r[yi] = dv[rsum]; g[yi] = dv[gsum]; b[yi] = dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - radius + div; sir = stack[stackstart % div]; routsum -= sir[0]; goutsum -= sir[1]; boutsum -= sir[2]; if (y == 0) { vmin[x] = Math.min(x + radius + 1, wm); } p = pix[yw + vmin[x]]; sir[0] = (p & 0xff0000) >> 16; sir[1] = (p & 0x00ff00) >> 8; sir[2] = (p & 0x0000ff); rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1) % div; sir = stack[(stackpointer) % div]; routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; rinsum -= sir[0]; ginsum -= sir[1]; binsum -= sir[2]; yi++; } yw += w; } for (x = 0; x < w; x++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; yp = -radius * w; for (i = -radius; i <= radius; i++) { yi = Math.max(0, yp) + x; sir = stack[i + radius]; sir[0] = r[yi]; sir[1] = g[yi]; sir[2] = b[yi]; rbs = r1 - Math.abs(i); rsum += r[yi] * rbs; gsum += g[yi] * rbs; bsum += b[yi] * rbs; if (i > 0) { rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; } else { routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; } if (i < hm) { yp += w; } } yi = x; stackpointer = radius; for (y = 0; y < h; y++) { // Preserve alpha channel: ( 0xff000000 & pix[yi] ) pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - radius + div; sir = stack[stackstart % div]; routsum -= sir[0]; goutsum -= sir[1]; boutsum -= sir[2]; if (x == 0) { vmin[y] = Math.min(y + r1, hm) * w; } p = x + vmin[y]; sir[0] = r[p]; sir[1] = g[p]; sir[2] = b[p]; rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1) % div; sir = stack[stackpointer]; routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; rinsum -= sir[0]; ginsum -= sir[1]; binsum -= sir[2]; yi += w; } } destBitmap.setPixels(pix, 0, w, 0, 0, w, h); }
From source file:com.yk.notification.util.BitmapUtil.java
/** * ?// w ww.j av a 2s .c om * * @param bmp * ? * @return ?? */ public static Bitmap convertToBlackWhite(Bitmap bmp) { int width = bmp.getWidth(); int height = bmp.getHeight(); int[] pixels = new int[width * height]; bmp.getPixels(pixels, 0, width, 0, 0, width, height); int alpha = 0xFF << 24; // bitmap?24 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int grey = pixels[width * i + j]; int red = ((grey & 0x00FF0000) >> 16); int green = ((grey & 0x0000FF00) >> 8); int blue = (grey & 0x000000FF); grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11); grey = alpha | (grey << 16) | (grey << 8) | grey; pixels[width * i + j] = grey; } } Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565); newBmp.setPixels(pixels, 0, width, 0, 0, width, height); return newBmp; }
From source file:freed.viewer.screenslide.ImageFragment.java
private void createHistogramm(Bitmap bitmap) { /*histogramData = new int [ 256*4]; RenderScript mRS = RenderScript.create(getContext()); ScriptIntrinsicHistogram histogram = ScriptIntrinsicHistogram.create(mRS,Element.U8_4(mRS)); Allocation mHistogramAllocation = Allocation.createSized(mRS, Element.I32_3(mRS), 256); Allocation in = Allocation.createFromBitmap(mRS, bitmap); histogram.setOutput(mHistogramAllocation); histogram.forEach(in);//from ww w . j a va 2 s. c om mHistogramAllocation.copyTo(histogramData);*/ Log.d(TAG, "Histodata"); if (bitmap == null || bitmap.isRecycled()) return; int[] histo = new int[256 * 3]; int w = bitmap.getWidth(); int h = bitmap.getHeight(); int[] pixels = new int[w * h]; bitmap.getPixels(pixels, 0, w, 0, 0, w, h); for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { int index = j * w + i; int r = Color.red(pixels[index]); int g = Color.green(pixels[index]); int b = Color.blue(pixels[index]); histo[r]++; histo[256 + g]++; histo[512 + b]++; } } pixels = null; histogramData = histo; }
From source file:com.ibm.mil.readyapps.physio.fragments.PainLocationFragment.java
/** * Pulls the bitmap image of the body part out of its RelativeLayout, colors it appropriatly, * and then tells the zoomLayout where it needs to zoom to. Also swaps the FRONT/BACK buttons * for the ADD/DELETE buttons.//from w w w . ja v a2s .c o m * * @param image the body part the touch occured in. */ private void zoomOnImage(RelativeLayout image) { Bitmap bitm = ((BitmapDrawable) image.getBackground()).getBitmap(); Bitmap temp = bitm.copy(bitm.getConfig(), true); int[] pixels = new int[temp.getHeight() * temp.getWidth()]; temp.getPixels(pixels, 0, temp.getWidth(), 0, 0, temp.getWidth(), temp.getHeight()); for (int i = 0; i < temp.getHeight() * temp.getWidth(); i++) { if (pixels[i] != 0) { int r, g, b; if (!blue) { r = Color.red(pixels[i]) + 100; if (r > 255) { r = 255; } g = Color.green(pixels[i]) - 50; b = Color.blue(pixels[i]) - 50; } else { r = Color.red(pixels[i]) - 30; g = Color.green(pixels[i]) + 15; b = Color.blue(pixels[i]) + 100; if (b > 255) { b = 255; } } pixels[i] = Color.argb(Color.alpha(pixels[i]), r, g, b); } } temp.setPixels(pixels, 0, temp.getWidth(), 0, 0, temp.getWidth(), temp.getHeight()); image.setBackground(new BitmapDrawable(getResources(), temp)); Rect rect = new Rect(); image.getGlobalVisibleRect(rect); zoomLayout.zoomToRect(rect, 0, getYOffset()); current = image; zoomed = true; swapButtons(); placePointer(rect.centerX(), rect.centerY()); }
From source file:com.htc.dotdesign.DrawingView.java
public void setAsDotViewTheme() { if (mContext == null) { return;//from ww w .j av a 2 s . c o m } Bitmap bitmap = null; Bitmap photoARGB = null; try { bitmap = handlePicture(); if (bitmap != null) { int[] photoData = new int[bitmap.getWidth() * bitmap.getHeight()]; bitmap.getPixels(photoData, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); photoARGB = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); photoARGB.setPixels(photoData, 0, photoARGB.getWidth(), 0, 0, photoARGB.getWidth(), photoARGB.getHeight()); WallpaperMaker wallpaper = new WallpaperMaker(); mScaledPhoto = wallpaper.convertDotViewWallpaper(photoARGB, 27, 48); ByteArrayOutputStream stream = new ByteArrayOutputStream(); mScaledPhoto.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); Intent intent = new Intent(DotDesignConstants.NOTIFY_DOTVIEW_TO_UPDATE_WALLPAPER); intent.putExtra(DotDesignConstants.EXTRA_BITMAP_ARRAY, byteArray); String filename = DotDesignUtil.getFileName() + "DotDrop"; intent.putExtra(DotDesignConstants.EXTRA_FILE_NAME, filename); mContext.sendBroadcast(intent); } } catch (Exception e) { e.printStackTrace(); } finally { if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); bitmap = null; } if (mScaledPhoto != null && !mScaledPhoto.isRecycled()) { mScaledPhoto.recycle(); mScaledPhoto = null; } if (photoARGB != null && !photoARGB.isRecycled()) { photoARGB.recycle(); photoARGB = null; } } if (mActivity != null) { Toast.makeText(mActivity, getResources().getString(R.string.toast_theme_applied), Toast.LENGTH_SHORT) .show(); } }
From source file:com.undatech.opaque.RemoteCanvas.java
void initializeSoftCursor() { Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.cursor); int w = bm.getWidth(); int h = bm.getHeight(); int[] tempPixels = new int[w * h]; bm.getPixels(tempPixels, 0, w, 0, 0, w, h); // Set cursor rectangle as well. myDrawable.setCursorRect(pointer.getX(), pointer.getY(), w, h, 0, 0); // Set softCursor to whatever the resource is. myDrawable.setSoftCursor(tempPixels); bm.recycle();//from ww w . j a v a 2s . c o m }