public byte[] getTileImageData(int x, int y, int zoom) { mStream.reset(); Matrix matrix = new Matrix(mBaseMatrix); float scale = (float) (Math.pow(2, zoom) * mScale); matrix.postScale(scale, scale); matrix.postTranslate(-x * mDimension, -y * mDimension); mBitmap.eraseColor(Color.TRANSPARENT); Canvas c = new Canvas(mBitmap); c.setMatrix(matrix); // NOTE: Picture is not thread-safe. synchronized (mSvgPicture) { mSvgPicture.draw(c); } BufferedOutputStream stream = new BufferedOutputStream(mStream); mBitmap.compress(Bitmap.CompressFormat.PNG, 0, stream); try { stream.close(); } catch (IOException e) { Log.e(TAG, "Error while closing tile byte stream."); e.printStackTrace(); } return mStream.toByteArray(); }
// ============================================================================== public final int[] renderGlyph( char glyph, Paint paint, android.graphics.Matrix matrix, Rect bounds) { Path p = new Path(); paint.getTextPath(String.valueOf(glyph), 0, 1, 0.0f, 0.0f, p); RectF boundsF = new RectF(); p.computeBounds(boundsF, true); matrix.mapRect(boundsF); boundsF.roundOut(bounds); bounds.left--; bounds.right++; final int w = bounds.width(); final int h = Math.max(1, bounds.height()); Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bm); matrix.postTranslate(-bounds.left, -bounds.top); c.setMatrix(matrix); c.drawPath(p, paint); final int sizeNeeded = w * h; if (cachedRenderArray.length < sizeNeeded) cachedRenderArray = new int[sizeNeeded]; bm.getPixels(cachedRenderArray, 0, w, 0, 0, w, h); bm.recycle(); return cachedRenderArray; }
public static Bitmap rotateBmp(Bitmap bmp, int angle) { int width = bmp.getWidth(); int height = bmp.getHeight(); Matrix matrix = new Matrix(); matrix.postRotate(angle); return Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true); }
public static Bitmap zoomImage(Bitmap bmp, double newWidth, double newHeight) { float width = bmp.getWidth(); float height = bmp.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; matrix.postScale(scaleWidth, scaleHeight); Bitmap bitmap = Bitmap.createBitmap(bmp, 0, 0, (int) width, (int) height, matrix, true); return bitmap; }
public static Bitmap matrixBmp(Bitmap bmp, int margin) { int width = bmp.getWidth(); int height = bmp.getHeight(); int px = UIUtils.dipToPx(margin); Matrix matrix = new Matrix(); if (width > UIUtils.getWidth() - px) { float scale = ((float) (UIUtils.getWidth() - px) / width); matrix.postScale(scale, scale); } else { matrix.postScale(1, 1); } return Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true); }
public static Bitmap colorMatrixBmp(Bitmap bmp, float[] matrixSrc) { int width = bmp.getWidth(); int height = bmp.getHeight(); ColorMatrix matrix = new ColorMatrix(); matrix.set(matrixSrc); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); Paint p = new Paint(); p.setColorFilter(filter); Bitmap colorBmp = Bitmap.createBitmap(width, height, bmp.getConfig()); Canvas c = new Canvas(colorBmp); c.drawBitmap(bmp, 0, 0, p); return colorBmp; }
public static Bitmap flipBmp(Bitmap bmp, int mode) { // mode 1: h, 2:v int width = bmp.getWidth(); int height = bmp.getHeight(); Matrix matrix = new Matrix(); Matrix temp = new Matrix(); float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1}; temp.setValues(mirrorY); matrix.postConcat(temp); if (mode == 2) { matrix.setRotate(180, width / 2, height / 2); } return Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true); }
public static Bitmap[] load(String src, int width, int height) { Bitmap[] texture = null; InputStream is = null; AssetManager am = resources.getAssets(); String[] nameOfBoxs = null; try { nameOfBoxs = am.list(src); } catch (IOException e) { Log.d("GraphicMaster", "Culd not find folder " + src); } Log.d("GraphicMaster", "NameOfBoxs.length = " + nameOfBoxs.length); texture = new Bitmap[nameOfBoxs.length]; for (int i = 0; i < nameOfBoxs.length; i++) { try { is = resources.getAssets().open(src + "/" + nameOfBoxs[i]); texture[i] = BitmapFactory.decodeStream(is); /* if(width > 0 && height > 0) texture[i] = Bitmap.createScaledBitmap(texture[i], width, height, false);*/ Log.d( "GraphicMaster", "Scaled from " + new Integer((int) (texture[i].getWidth())).toString() + " " + new Integer((int) (texture[i].getHeight())).toString() + " to " + new Integer((int) (texture[i].getWidth() * GameSettings.widthM)).toString() + " " + new Integer((int) (GameSettings.heightM * texture[i].getWidth())).toString()); Log.d( "GraphicMaster", "Scale" + new Double(GameSettings.widthM).toString() + " " + new Double(GameSettings.heightM).toString()); if (GameSettings.widthM != 1 && GameSettings.heightM != 1) texture[i] = Bitmap.createScaledBitmap( texture[i], (int) (texture[i].getWidth() * GameSettings.widthM), (int) (texture[i].getHeight() * GameSettings.heightM), false); } catch (IOException e) { Log.d("GraphicMaster", "Culd not read: " + src + nameOfBoxs[i]); } } return texture; }
public static Bitmap roundedCornerBitmap(Bitmap bitmap, float radis) { 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()); final RectF rectF = new RectF(rect); final float roundPx = radis; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.WHITE); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
/** Attempt to free memory and remove references. */ public void cleanUp() { mBitmap.recycle(); mBitmap = null; try { mStream.close(); } catch (IOException e) { // ignore } mStream = null; }
public static Bitmap blackWhiteBmp(Bitmap bmp) { // black and white int width = bmp.getWidth(); int height = bmp.getHeight(); ColorMatrix matrix = new ColorMatrix(); float[] src = { 0.308F, 0.609F, 0.082F, 0, 0, 0.308F, 0.609F, 0.082F, 0, 0, 0.308F, 0.609F, 0.082F, 0, 0, 0, 0, 0, 1, 0 }; matrix.set(src); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); Paint p = new Paint(); p.setColorFilter(filter); Bitmap colorBmp = Bitmap.createBitmap(width, height, bmp.getConfig()); Canvas c = new Canvas(colorBmp); c.drawBitmap(bmp, 0, 0, p); return colorBmp; }
public static Drawable zoomDrawable(Drawable drawable, int w, int h) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap oldbmp = drawableToBitmap(drawable); Matrix matrix = new Matrix(); float sx = ((float) w / width); float sy = ((float) h / height); matrix.postScale(sx, sy); Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true); return new BitmapDrawable(newbmp); }
public static void saveBitmapToFile(Bitmap bmp, String fileName, CompressFormat format) { try { File f = new File(fileName); f.createNewFile(); FileOutputStream fOut = null; fOut = new FileOutputStream(f); bmp.compress(format, 100, fOut); fOut.flush(); fOut.close(); } catch (Exception e) { } }
public static Bitmap drawableToBitmap(Drawable drawable) { int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; Bitmap bitmap = Bitmap.createBitmap(w, h, config); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); drawable.draw(canvas); return bitmap; }
public static Bitmap blurBmp(Bitmap bmp, int Blur) { int pixels[] = new int[bmp.getWidth() * bmp.getHeight()]; int pixelsRawSource[] = new int[bmp.getWidth() * bmp.getHeight() * 3]; int pixelsRawNew[] = new int[bmp.getWidth() * bmp.getHeight() * 3]; bmp.getPixels(pixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight()); for (int k = 1; k <= Blur; k++) { for (int i = 0; i < pixels.length; i++) { pixelsRawSource[i * 3 + 0] = Color.red(pixels[i]); pixelsRawSource[i * 3 + 1] = Color.green(pixels[i]); pixelsRawSource[i * 3 + 2] = Color.blue(pixels[i]); } int CurrentPixel = bmp.getWidth() * 3 + 3; for (int i = 0; i < bmp.getHeight() - 3; i++) { for (int j = 0; j < bmp.getWidth() * 3; j++) { CurrentPixel += 1; int sumColor = 0; sumColor = pixelsRawSource[CurrentPixel - bmp.getWidth() * 3]; sumColor = sumColor + pixelsRawSource[CurrentPixel - 3]; sumColor = sumColor + pixelsRawSource[CurrentPixel + 3]; sumColor = sumColor + pixelsRawSource[CurrentPixel + bmp.getWidth() * 3]; pixelsRawNew[CurrentPixel] = Math.round(sumColor / 4); } } for (int i = 0; i < pixels.length; i++) { pixels[i] = Color.rgb(pixelsRawNew[i * 3 + 0], pixelsRawNew[i * 3 + 1], pixelsRawNew[i * 3 + 2]); } } Bitmap bmpReturn = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Config.ARGB_8888); bmpReturn.setPixels(pixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight()); return bmpReturn; }
public TileGenerator() { mBitmap = Bitmap.createBitmap(mDimension, mDimension, Bitmap.Config.ARGB_8888); mStream = new ByteArrayOutputStream(mDimension * mDimension * 4); }