Esempio n. 1
0
  // ==============================================================================
  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;
  }
Esempio n. 2
0
  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);
  }
Esempio n. 3
0
 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;
 }
Esempio n. 4
0
 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);
 }
Esempio n. 5
0
  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);
  }
Esempio n. 6
0
  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;
  }
Esempio n. 7
0
  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);
  }
Esempio n. 8
0
  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;
  }
Esempio n. 9
0
  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;
  }
Esempio n. 10
0
  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;
  }
Esempio n. 11
0
  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;
  }
Esempio n. 12
0
 public TileGenerator() {
   mBitmap = Bitmap.createBitmap(mDimension, mDimension, Bitmap.Config.ARGB_8888);
   mStream = new ByteArrayOutputStream(mDimension * mDimension * 4);
 }