// 该函数实现对图像进行二值化处理 public Bitmap gray2Binary(Bitmap graymap) { // 得到图形的宽度和长度 int width = graymap.getWidth(); int height = graymap.getHeight(); // 创建二值化图像 Bitmap binarymap = null; binarymap = graymap.copy(Config.ARGB_8888, true); // 依次循环,对图像的像素进行处理 for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { // 得到当前像素的值 int col = binarymap.getPixel(i, j); // 得到alpha通道的值 int alpha = col & 0xFF000000; // 得到图像的像素RGB的值 int red = (col & 0x00FF0000) >> 16; int green = (col & 0x0000FF00) >> 8; int blue = (col & 0x000000FF); // 用公式X = 0.3×R+0.59×G+0.11×B计算出X代替原来的RGB int gray = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11); // 对图像进行二值化处理 if (gray <= 95) { gray = 0; } else { gray = 255; } // 新的ARGB int newColor = alpha | (gray << 16) | (gray << 8) | gray; // 设置新图像的当前像素值 binarymap.setPixel(i, j, newColor); } } return binarymap; }
public void setColorToNumber(Bitmap number, int color) { for (int col = 0; col < number.getWidth(); col++) { for (int row = 0; row < number.getHeight(); row++) { number.setPixel(col, row, (number.getPixel(col, row) & 0xFF000000) | (0x00FFFFFF & color)); } } }
public static Bitmap doColorFilter(Bitmap src, double red, double green, double blue) { // image size int width = src.getWidth(); int height = src.getHeight(); // create output bitmap Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig()); // color information int A, R, G, B; int pixel; // scan through all pixels for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { // get pixel color pixel = src.getPixel(x, y); // apply filtering on each channel R, G, B A = Color.alpha(pixel); R = (int) (Color.red(pixel) * red); G = (int) (Color.green(pixel) * green); B = (int) (Color.blue(pixel) * blue); // set new color pixel to output bitmap bmOut.setPixel(x, y, Color.argb(A, R, G, B)); } } // return final image return bmOut; }
public void plot_camera_image_rgb( Canvas plotCanvas, byte[] latestRGBImage, int imageWidth, int imageHeight, int width, int height) { if (bm == null) bm = Bitmap.createBitmap(imageWidth / 8, imageHeight / 4, Bitmap.Config.ARGB_8888); for (int y = 0; y < imageHeight / 4; y++) for (int x = 0; x < imageWidth / 8; x++) { int i = (y * 4 * imageWidth + x * 4) * 3; int rgbColor = 0xFF000000 | (latestRGBImage[i] << 16) & 0xFF0000 | (latestRGBImage[i + 1] << 8) & 0xFF00 | latestRGBImage[i + 2] & 0xFF; bm.setPixel(x, y, rgbColor); } Rect src = new Rect(0, 0, imageHeight / 4, imageWidth / 8); Rect dst = new Rect(0, 0, width, height); plotCanvas.rotate(90, width / 2, height / 2); plotCanvas.drawBitmap(bm, src, dst, null); }
@Test public void shouldSetPixel() { Bitmap bitmap = Bitmap.createBitmap(new int[] {1}, 1, 1, Config.ARGB_8888); shadowOf(bitmap).setMutable(true); bitmap.setPixel(0, 0, 2); assertThat(bitmap.getPixel(0, 0)).isEqualTo(2); assertThat(shadowOf(bitmap).getCreatedFromColors()).isEqualTo(new int[] {1}); }
@Test public void shouldSetPixel_allocateOnTheFly() { Bitmap bitmap = Bitmap.createBitmap(1, 1, Config.ARGB_8888); shadowOf(bitmap).setMutable(true); bitmap.setPixel(0, 0, 2); assertThat(bitmap.getPixel(0, 0)).isEqualTo(2); assertThat(shadowOf(bitmap).getCreatedFromColors()).isNull(); }
private Bitmap getPreviewBitmap() { int d = (int) (mDensity * 31); // 30dip int color = getValue(); Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888); int w = bm.getWidth(); int h = bm.getHeight(); int c; for (int i = 0; i < w; i++) { for (int j = i; j < h; j++) { c = (i <= 1 || j <= 1 || i >= w - 2 || j >= h - 2) ? Color.GRAY : color; bm.setPixel(i, j, c); if (i != j) { bm.setPixel(j, i, c); } } } return bm; }
void updatePattern() { // Log.d(LOG, "Filter pattern " + Cfg.Pattern); int shift = getShift(); int shiftX = shift % Grids.GridSideSize; int shiftY = shift / Grids.GridSideSize; for (int i = 0; i < Grids.GridSize; i++) { int x = (i + shiftX) % Grids.GridSideSize; int y = ((i / Grids.GridSideSize) + shiftY) % Grids.GridSideSize; int color = (Grids.Patterns[Cfg.Pattern][i] == 0) ? Color.TRANSPARENT : Color.BLACK; bmp.setPixel(x, y, color); } }
private Bitmap rotateBitmap(Bitmap bitmap) { Bitmap resBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); for (int i = 0; i < bitmap.getHeight(); i++) { for (int j = 0; j < bitmap.getWidth(); j++) { resBitmap.setPixel(j, i, bitmap.getPixel(i, j)); } } return resBitmap; }
public static Bitmap generate(String data) throws WriterException { Bitmap bmp; QRCodeWriter writer = new QRCodeWriter(); BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, 512, 512); int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); } } return bmp; }
public void testPngStaysSame() { Bitmap bitmap1 = mCoverImage.copy(Config.ARGB_8888, true); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap1.setPixel(1, 1, 0xFFFF0F0F); assertEquals(0xFFFF0F0F, bitmap1.getPixel(1, 1)); bitmap1.compress(CompressFormat.PNG, 100, bos); byte[] ba = bos.toByteArray(); Bitmap bitmap2 = BitmapFactory.decodeByteArray(ba, 0, ba.length); assertEquals(bitmap1.getPixel(1, 1), bitmap2.getPixel(1, 1)); }
private Bitmap mirrorBitmap(Bitmap bitmap) { Bitmap resBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); int n = bitmap.getHeight(), m = bitmap.getWidth(); for (int i = 0; i < n / 2; i++) { for (int j = 0; j < m; j++) { resBitmap.setPixel(i, j, bitmap.getPixel(n - i - 1, j)); resBitmap.setPixel(n - i - 1, j, bitmap.getPixel(i, j)); } } return resBitmap; }
public void setPixelsRandom(int width, int height) { Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bitmap.setPixel( x, y, Color.rgb( (int) (255.0 * Math.random()), (int) (255.0 * Math.random()), (int) (255.0 * Math.random()))); } } setPixels(bitmap); }
@Override public Bitmap call(sensor_msgs.Image message) { Preconditions.checkArgument(message.getEncoding().equals("rgb8")); Bitmap bitmap = Bitmap.createBitmap( (int) message.getWidth(), (int) message.getHeight(), Bitmap.Config.ARGB_8888); for (int x = 0; x < message.getWidth(); x++) { for (int y = 0; y < message.getHeight(); y++) { ChannelBuffer data = message.getData(); byte red = data.getByte((int) (y * message.getStep() + 3 * x)); byte green = data.getByte((int) (y * message.getStep() + 3 * x + 1)); byte blue = data.getByte((int) (y * message.getStep() + 3 * x + 2)); bitmap.setPixel(x, y, Color.argb(255, red & 0xFF, green & 0xFF, blue & 0xFF)); } } return bitmap; }
/** * Get the internal picture that was used during the OCR. The OCR engine uses a filtered image, * and not the original. Using this function, one can see why there were problems during OCR. * * @param pic_width the picture width * @param pic_height the picture height * @return the internal image as a bitmap */ public Bitmap GetInternImage(int pic_width, int pic_height) { // from disk file Log.v(TAG, "getInternImage w=" + pic_width + " h=" + pic_height); byte[] tif = null; try { tif = GetBytesFromFile( new File(Mezzofanti.RESULTS_PATH + "tessinput.tif"), pic_width, pic_height, 1); } catch (Exception ex) { Log.v(TAG, "exception: " + ex.toString()); } if (tif == null) { Log.v(TAG, "warning: tif is null"); return null; } Bitmap bmp2 = null; try { bmp2 = Bitmap.createBitmap(pic_width, pic_height, Bitmap.Config.RGB_565); } catch (Throwable th) { Log.v(TAG, "throwable: " + th.toString()); return null; } int[] pixbuf = new int[8]; for (int h = 0; h < pic_height; h++) for (int w = 0; w < pic_width; w++) { if (w % 8 == 0) { // read 8b -> 8pixels int pix = tif[258 + (h * pic_width + w) / 8]; // store them in a buffer for (int i = 0; i < 8; i++) { pix = pix >> i; pixbuf[7 - i] = pix & 0x00000001; if (pixbuf[7 - i] != 0) pixbuf[7 - i] = 0x00ffffff; } } bmp2.setPixel(w, h, pixbuf[w % 8]); } return bmp2; }
// Converts image to grayscale (NEW) public Bitmap toGrayScale(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight(); width = bmpOriginal.getWidth(); Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { int color = bmpOriginal.getPixel(x, y); int r = (color >> 16) & 0xFF; int g = (color >> 8) & 0xFF; int b = color & 0xFF; int gray = (r + g + b) / 3; color = Color.rgb(gray, gray, gray); // color = Color.rgb(r/3, g/3, b/3); bmpGrayscale.setPixel(x, y, color); } } return bmpGrayscale; }
public Bitmap drawPuddles() { int numrows = dem.length; int numcols = dem[0].length; int[] colorarray = new int[this.pits.pitIdMatrix.length * this.pits.pitIdMatrix[0].length]; Arrays.fill(colorarray, Color.TRANSPARENT); Bitmap.Config config = Bitmap.Config.ARGB_8888; Bitmap puddleBitmap = Bitmap.createBitmap(colorarray, numcols, numrows, config); puddleBitmap = puddleBitmap.copy(config, true); for (int r = 1; r < numrows - 1; r++) { for (int c = 1; c < numcols - 1; c++) { if (r >= numrows - 1 || r <= 0 || c >= numcols - 1 || c <= 0) { continue; } if (originalDem[r][c] < dem[r][c]) { puddleBitmap.setPixel(numcols - 1 - c, r, Color.BLUE); } } } return puddleBitmap; }
public Bitmap generateQR(String text) { Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x * 3 / 4; int height = width; MultiFormatWriter writer = new MultiFormatWriter(); Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q); BitMatrix matrix = null; Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); try { matrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hints); } catch (WriterException e) { return bmp; } for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE); } } return bmp; }
public void plot_camera_image( Canvas plotCanvas, byte[] image, int imageWidth, int imageHeight, int width, int height) { if (bm == null) bm = Bitmap.createBitmap(imageWidth / 8, imageHeight / 4, Bitmap.Config.ARGB_8888); for (int y = 0; y < imageHeight / 4; y++) for (int x = 0; x < imageWidth / 8; x++) { int i = (y * 4 * imageWidth + x * 4); int greyValue = image[i]; int greyColor = 0xFF000000 | (greyValue << 16) & 0xFF0000 | (greyValue << 8) & 0xFF00 | greyValue & 0xFF; bm.setPixel(x, y, greyColor); } // plotCanvas.drawBitmap(bm, 0, 0, null); Rect src = new Rect(0, 0, imageHeight / 4, imageWidth / 8); Rect dst = new Rect(0, 0, width, height); plotCanvas.rotate(90, width / 2, height / 2); plotCanvas.drawBitmap(bm, src, dst, null); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionForSetPixelOnImmutableBitmap() { Bitmap bitmap = Bitmap.createBitmap(new int[] {1}, 1, 1, Config.ARGB_8888); bitmap.setPixel(0, 0, 2); }
public void takeImageData(int[] array, int clientid, int datachunk) throws BusException { if (clientid != -1) { total_packets++; try { // System.out.println("Received chunk "+datachunk); double x = decisiontable[index][2] / fmax; int serverstartindex = (int) (pixels.length * (1 - x)); System.arraycopy( array, 0, smoothed_pixels, (clientid * serverstartindex / (no_of_clients_needed)) + (datachunk * size_of_each_chunk), size_of_each_chunk); chunks_recd++; // the following lines added for facebook hackathon Bitmap image = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { image.setPixel(j, i, smoothed_pixels[i * w + j]); } } // FileOutputStream fout=new FileOutputStream("/storage/sdcard0/received_image.png"); if (completelyZero(smoothed_pixels) != 1) { FileOutputStream fout = new FileOutputStream("/storage/sdcard0/received_image.png"); image.compress(Bitmap.CompressFormat.PNG, 100, fout); fout.flush(); fout.close(); } // the *5 part in the next line was added for facebook hackathon if (chunks_recd == (no_of_times_client_calls_image_data) * no_of_clients_needed * 10) { // possible infinite loop situation while (server_done != 1) { // System.out.println("Infinite loop!"); } image = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { image.setPixel(j, i, smoothed_pixels[i * w + j]); } } if (completelyZero(smoothed_pixels) != 1) { // FileOutputStream fout=new FileOutputStream("/storage/sdcard0/received_image.png"); FileOutputStream fout = new FileOutputStream("/storage/sdcard0/received_image.png"); image.compress(Bitmap.CompressFormat.PNG, 100, fout); fout.flush(); fout.close(); } timeafter = System.currentTimeMillis(); // for facebook hackathon System.out.println(" "); System.out.println("everything done!. Time: " + (timeafter - timebefore) + " ms."); System.out.println(" "); timer_flag = 0; } } catch (Exception e) { e.printStackTrace(); } } }
@SuppressWarnings("unchecked") public TabbedMainView(Object params) { Context ctx = RhodesActivity.getContext(); mBackgroundColorEnable = false; Vector<Object> tabs = null; boolean place_tabs_bottom = false; if (params instanceof Vector<?>) tabs = (Vector<Object>) params; else if (params instanceof Map<?, ?>) { Map<Object, Object> settings = (Map<Object, Object>) params; Object bkgObj = settings.get("background_color"); if ((bkgObj != null) && (bkgObj instanceof String)) { int color = Integer.parseInt((String) bkgObj) | 0xFF000000; mBackgroundColor = color; mBackgroundColorEnable = true; } Object callbackObj = settings.get("on_change_tab_callback"); if ((callbackObj != null) && (callbackObj instanceof String)) { mChangeTabCallback = new String(((String) callbackObj)); } Object placeBottomObj = settings.get("place_tabs_bottom"); if ((placeBottomObj != null) && (placeBottomObj instanceof String)) { place_tabs_bottom = ((String) placeBottomObj).equalsIgnoreCase("true"); } Object tabsObj = settings.get("tabs"); if (tabsObj != null && (tabsObj instanceof Vector<?>)) tabs = (Vector<Object>) tabsObj; } if (tabs == null) throw new IllegalArgumentException("No tabs specified"); int size = tabs.size(); host = new TabHost(ctx, null); tabData = new Vector<TabData>(size); tabIndex = 0; TabWidget tabWidget = new TabWidget(ctx); tabWidget.setId(android.R.id.tabs); FrameLayout frame = new FrameLayout(ctx); FrameLayout.LayoutParams lpf = null; TabHost.LayoutParams lpt = null; if (place_tabs_bottom) { frame.setId(android.R.id.tabcontent); lpf = new FrameLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.TOP); host.addView(frame, lpf); lpt = new TabHost.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM); host.addView(tabWidget, lpt); } else { lpt = new TabHost.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, Gravity.TOP); host.addView(tabWidget, lpt); frame = new FrameLayout(ctx); frame.setId(android.R.id.tabcontent); lpf = new FrameLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.BOTTOM); host.addView(frame, lpf); } host.setup(); TabHost.TabSpec spec; DisplayMetrics metrics = new DisplayMetrics(); WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE); wm.getDefaultDisplay().getMetrics(metrics); int selected_color = 0; boolean selected_color_enable = false; for (int i = 0; i < size; ++i) { Object param = tabs.elementAt(i); if (!(param instanceof Map<?, ?>)) throw new IllegalArgumentException("Hash expected"); Map<Object, Object> hash = (Map<Object, Object>) param; Object labelObj = hash.get("label"); if (labelObj == null || !(labelObj instanceof String)) throw new IllegalArgumentException("'label' should be String"); Object actionObj = hash.get("action"); boolean use_current_view_for_tab = false; Object use_current_view_for_tab_Obj = hash.get("use_current_view_for_tab"); if (use_current_view_for_tab_Obj != null) { use_current_view_for_tab = ((String) use_current_view_for_tab_Obj).equalsIgnoreCase("true"); } if (use_current_view_for_tab) { actionObj = new String("none"); } if (actionObj == null || !(actionObj instanceof String)) throw new IllegalArgumentException("'action' should be String"); String label = (String) labelObj; String action = (String) actionObj; String icon = null; boolean reload = false; boolean disabled = false; int web_bkg_color = 0xFFFFFFFF; Object iconObj = hash.get("icon"); if (iconObj != null && (iconObj instanceof String)) icon = "apps/" + (String) iconObj; Object reloadObj = hash.get("reload"); if (reloadObj != null && (reloadObj instanceof String)) reload = ((String) reloadObj).equalsIgnoreCase("true"); Object selected_color_Obj = hash.get("selected_color"); if ((selected_color_Obj != null) && (selected_color_Obj instanceof String)) { selected_color_enable = true; selected_color = Integer.parseInt((String) selected_color_Obj) | 0xFF000000; } Object disabled_Obj = hash.get("disabled"); if (disabled_Obj != null && (disabled_Obj instanceof String)) disabled = ((String) disabled_Obj).equalsIgnoreCase("true"); Object web_bkg_color_Obj = hash.get("web_bkg_color"); if (web_bkg_color_Obj != null && (web_bkg_color_Obj instanceof String)) { web_bkg_color = Integer.parseInt((String) web_bkg_color_Obj) | 0xFF000000; } spec = host.newTabSpec(Integer.toString(i)); // Set label and icon BitmapDrawable drawable = null; if (icon != null) { String iconPath = RhoFileApi.normalizePath(icon); Bitmap bitmap = BitmapFactory.decodeStream(RhoFileApi.open(iconPath)); if (disabled && (bitmap != null)) { // replace Bitmap to gray bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); // prepare mutable bitmap int x; int y; int bw = bitmap.getWidth(); int bh = bitmap.getHeight(); int nc = DISABLED_IMG_COLOR & 0xFFFFFF; int c; for (y = 0; y < bh; y++) { for (x = 0; x < bw; x++) { c = bitmap.getPixel(x, y); c = nc | (c & 0xFF000000); bitmap.setPixel(x, y, c); } } } if (bitmap != null) bitmap.setDensity(DisplayMetrics.DENSITY_MEDIUM); // Bitmap.DENSITY_NONE); drawable = new BitmapDrawable(bitmap); drawable.setTargetDensity(metrics); } if (drawable == null) spec.setIndicator(label); else spec.setIndicator(label, drawable); SimpleMainView view = null; if (use_current_view_for_tab) { RhodesService r = RhodesService.getInstance(); MainView mainView = r.getMainView(); action = mainView.currentLocation(-1); view = new SimpleMainView(mainView); } else { view = new SimpleMainView(); } // Set view factory if (web_bkg_color_Obj != null) { if (!use_current_view_for_tab) { view.setWebBackgroundColor(web_bkg_color); } host.setBackgroundColor(web_bkg_color); } TabData data = new TabData(); data.view = view; data.url = action; data.reload = reload; if (use_current_view_for_tab) { data.loaded = true; tabIndex = i; } data.selected_color = selected_color; data.selected_color_enabled = selected_color_enable; data.disabled = disabled; TabViewFactory factory = new TabViewFactory(data); spec.setContent(factory); tabData.addElement(data); host.addTab(spec); } tabWidget.measure(host.getWidth(), host.getHeight()); int hh = tabWidget.getMeasuredHeight(); // if (hh < 64) { // hh = 64; // } if (place_tabs_bottom) { lpf.setMargins(0, 0, 0, hh); } else { lpf.setMargins(0, hh, 0, 0); } host.updateViewLayout(frame, lpf); }
private static Bitmap sobel(Bitmap referenceImage) { int blurFactor = 2; /* Blur the image by changing the size */ Bitmap smallerOrig = Bitmap.createScaledBitmap( referenceImage, referenceImage.getWidth() / blurFactor, referenceImage.getHeight() / blurFactor, true); Bitmap blurredOrig = Bitmap.createScaledBitmap( smallerOrig, smallerOrig.getWidth() * blurFactor, smallerOrig.getHeight() * blurFactor, true); smallerOrig.recycle(); int width = blurredOrig.getWidth(); int height = blurredOrig.getHeight(); Bitmap edges = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); int[][] grays = new int[width][height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { grays[i][j] = -1; } } int[] bins = new int[256]; int binsCount = 0; int max = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int rSum1 = 0, rSum2 = 0, rSum3 = 0, rSum4 = 0; int dSum1 = 0, dSum2 = 0, dSum3 = 0, dSum4 = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int xi = x + i, yj = y + j; if (xi >= 0 && xi < width && yj >= 0 && yj < height) { int gray = grays[xi][yj]; if (gray < 0) { gray = rgb2gray(blurredOrig.getPixel(xi, yj)); grays[xi][yj] = gray; } rSum1 += gray * SOBEL_ROW[i + 1][j + 1]; dSum1 += gray * SOBEL_DIAG[i + 1][j + 1]; rSum2 += gray * SOBEL_ROW[1 - i][1 - j]; dSum2 += gray * SOBEL_DIAG[1 - i][1 - j]; rSum3 += gray * SOBEL_ROW[1 - i][j + 1]; dSum3 += gray * SOBEL_DIAG[1 - i][j + 1]; rSum4 += gray * SOBEL_ROW[i + 1][1 - j]; dSum4 += gray * SOBEL_DIAG[i + 1][1 - j]; } } } int myGray = (Math.max( rSum1, Math.max( dSum1, Math.max( rSum2, Math.max( dSum2, Math.max(rSum3, Math.max(dSum3, Math.max(rSum4, dSum4)))))))) & 0xFF; if (myGray > max) { max = myGray; } if (myGray > 0) { bins[myGray]++; binsCount++; } int newPixel = (myGray << 24) | 0x00FFFFFF; edges.setPixel(x, y, newPixel); } } blurredOrig.recycle(); /* Find good gray cut value and remove pixels with intensity smaller than this cut */ int currentCount = 0; int grayCut = 0; int countCut = (int) (binsCount * 8.0 / 10.0); for (int i = 0; i < max; i++) { currentCount += bins[i]; if (currentCount >= countCut) { grayCut = i; break; } } for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int pixel = edges.getPixel(x, y); if (((pixel >> 24) & 0xFF) < grayCut) { edges.setPixel(x, y, 0x00FFFFFF); } } } return edges; }
public Bitmap delineate(Point point, AsyncTask task) { if (task instanceof DelineationListener) { delineationListener = (DelineationListener) task; } else { throw new ClassCastException("WatershedDataset - Task must implement DelineationListener"); } int numrows = this.dem.length; int numcols = this.dem[0].length; BitmapFactory.Options options = new BitmapFactory.Options(); options.inPurgeable = true; options.inInputShareable = true; Bitmap icon = BitmapFactory.decodeResource( MainActivity.context.getResources(), R.drawable.watershedelineation, options); Bitmap delinBitmap = Bitmap.createScaledBitmap(icon, numcols, numrows, false); for (int r = 1; r < numrows - 1; r++) { for (int c = 1; c < numcols - 1; c++) { delinBitmap.setPixel(numcols - 1 - c, r, Color.TRANSPARENT); } } delineatedArea = 0; // number of cells in the delineation delineatedStorageVolume = 0.0; // discover adjacent points that may be part of a puddle List<Point> indicesToCheck = new ArrayList<Point>(); List<Point> indicesToCheckPuddle = new ArrayList<Point>(); float puddleElevation = this.dem[point.y][point.x]; indicesToCheck.add(point); indicesToCheckPuddle.add(point); while (!indicesToCheckPuddle.isEmpty()) { int r = indicesToCheckPuddle.get(0).y; int c = indicesToCheckPuddle.get(0).x; indicesToCheckPuddle.remove(0); if (delinBitmap.getPixel(numcols - 1 - c, r) == Color.RED) { continue; } delinBitmap.setPixel(numcols - 1 - c, r, Color.RED); delineatedArea++; delineatedStorageVolume += dem[r][c] - originalDem[r][c]; for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { if (x == 0 && y == 0) { continue; } if (r + y >= numrows - 1 || r + y <= 0 || c + x >= numcols - 1 || c + x <= 0) { continue; } if (dem[r + y][c + x] == puddleElevation && delinBitmap.getPixel(numcols - 1 - (c + x), (r + y)) != Color.RED) { indicesToCheckPuddle.add(new Point(c + x, r + y)); indicesToCheck.add(new Point(c + x, r + y)); } } } } // Add a buffer around the chosen pixel to provide a more likely meaningful delineation for (int x = -3; x < 4; x++) { for (int y = -3; y < 4; y++) { if (point.y + y >= numrows - 1 || point.y + y <= 0 || point.x + x >= numcols - 1 || point.x + x <= 0) { continue; } if (delinBitmap.getPixel(numcols - 1 - (point.x + x), (point.y + y)) != Color.RED) { indicesToCheck.add(new Point(x + point.x, y + point.y)); delinBitmap.setPixel(numcols - 1 - (point.x + x), (point.y + y), Color.RED); delineatedArea++; delineatedStorageVolume += dem[point.y + y][point.x + x] - originalDem[point.y + y][point.x + x]; } } } // Now find all cells draining to either the puddle or the buffered delineation point while (!indicesToCheck.isEmpty()) { int r = indicesToCheck.get(0).y; int c = indicesToCheck.get(0).x; indicesToCheck.remove(0); if (flowDirection[r][c].parentList.isEmpty()) { continue; } for (int i = 0; i < flowDirection[r][c].parentList.size(); i++) { if (delinBitmap.getPixel( numcols - 1 - flowDirection[r][c].parentList.get(i).x, flowDirection[r][c].parentList.get(i).y) != Color.RED) { indicesToCheck.add(flowDirection[r][c].parentList.get(i)); delinBitmap.setPixel( numcols - 1 - flowDirection[r][c].parentList.get(i).x, flowDirection[r][c].parentList.get(i).y, Color.RED); delineatedArea++; delineatedStorageVolume += dem[r][c] - originalDem[r][c]; } } } return delinBitmap; }
public Bitmap takePicture() { Bitmap bm = null; byte[] ba; if (getCommands != null) { ba = getCommands.getPictureArray(); bm = Bitmap.createBitmap(256, 192, Bitmap.Config.ARGB_8888); int w = 256; int h = 192; int vy, vu, y1v, y1u, uy, uv, y2u, y2v; int V = 0, Y = 0, U = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (j >= 3) { vy = -1; vu = 2; y1v = -1; y1u = -3; uy = -1; uv = -2; y2u = -1; y2v = -3; } else { vy = 1; vu = 2; y1v = 3; y1u = 1; uy = 1; uv = 2; y2u = 3; y2v = 1; } if ((j % 4) == 0) { V = ba[i * w + j] & 0xff; Y = ba[i * w + j + vy] & 0xff; U = ba[i * w + j + vu] & 0xff; } else if ((j % 4) == 1) { Y = ba[i * w + j] & 0xff; V = ba[i * w + j + y1v] & 0xff; U = ba[i * w + j + y1u] & 0xff; } else if ((j % 4) == 2) { U = ba[i * w + j] & 0xff; Y = ba[i * w + j + uy] & 0xff; V = ba[i * w + j + uv] & 0xff; } else if ((j % 4) == 3) { Y = ba[i * w + j] & 0xff; U = ba[i * w + j + y2u] & 0xff; V = ba[i * w + j + y2v] & 0xff; } U = U - 128; V = V - 128; // Y = Y; bm.setPixel( j, i, Color.rgb( (int) Math.max(Math.min(Y + 1.13983 * V, 255), 0), (int) Math.max(Math.min(Y - 0.39466 * U - 0.58060 * V, 255), 0), (int) Math.max(Math.min(Y + 2.03211 * U, 255), 0))); } } } return bm; }
public static Bitmap blurImage(Bitmap bmp) { if (ImageCache.get("blurImage") != null) { return ImageCache.get("blurImage"); } int width = bmp.getWidth(); int height = bmp.getHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); int pixColor = 0; int newR = 0; int newG = 0; int newB = 0; int newColor = 0; int[][] colors = new int[9][3]; for (int i = 1, length = width - 1; i < length; i++) { for (int k = 1, len = height - 1; k < len; k++) { for (int m = 0; m < 9; m++) { int s = 0; int p = 0; switch (m) { case 0: s = i - 1; p = k - 1; break; case 1: s = i; p = k - 1; break; case 2: s = i + 1; p = k - 1; break; case 3: s = i + 1; p = k; break; case 4: s = i + 1; p = k + 1; break; case 5: s = i; p = k + 1; break; case 6: s = i - 1; p = k + 1; break; case 7: s = i - 1; p = k; break; case 8: s = i; p = k; } pixColor = bmp.getPixel(s, p); colors[m][0] = Color.red(pixColor); colors[m][1] = Color.green(pixColor); colors[m][2] = Color.blue(pixColor); } for (int m = 0; m < 9; m++) { newR += colors[m][0]; newG += colors[m][1]; newB += colors[m][2]; } newR = (int) (newR / 9F); newG = (int) (newG / 9F); newB = (int) (newB / 9F); newR = Math.min(255, Math.max(0, newR)); newG = Math.min(255, Math.max(0, newG)); newB = Math.min(255, Math.max(0, newB)); newColor = Color.argb(255, newR, newG, newB); bitmap.setPixel(i, k, newColor); newR = 0; newG = 0; newB = 0; } } ImageCache.put("blurImage", bitmap); return bitmap; }