android.graphics.Bitmap.Config Java Examples

The following examples show how to use android.graphics.Bitmap.Config. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImageUtility.java    From CameraV with GNU General Public License v3.0 6 votes vote down vote up
public static Bitmap drawableToBitmap(Drawable d) {
	if(d instanceof BitmapDrawable) {
		return ((BitmapDrawable) d).getBitmap();
	}
	
	int w = d.getIntrinsicWidth();
	w = w > 0 ? w : 1;
	
	int h = d.getIntrinsicHeight();
	h = h > 0 ? h : 1;
	
	Bitmap b = Bitmap.createBitmap(w, h, Config.ARGB_8888);
	Canvas c = new Canvas(b);
	d.setBounds(0, 0, c.getWidth(), c.getHeight());
	d.draw(c);
	
	return b;
}
 
Example #2
Source File: RoundedPagerDrawable.java    From letv with Apache License 2.0 6 votes vote down vote up
public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }
    try {
        Bitmap bitmap = Bitmap.createBitmap(Math.max(drawable.getIntrinsicWidth(), 2), Math.max(drawable.getIntrinsicHeight(), 2), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
        Log.w(TAG, "Failed to create bitmap from drawable!");
        return null;
    }
}
 
Example #3
Source File: BlurUtils.java    From letv with Apache License 2.0 6 votes vote down vote up
@SuppressLint({"NewApi"})
public static void blur_3(Context context, Bitmap bkg, View view) {
    long startMs = System.currentTimeMillis();
    Display d = ((Activity) context).getWindowManager().getDefaultDisplay();
    Bitmap overlay = Bitmap.createBitmap((int) (((float) d.getWidth()) / 1.0f), (int) (((float) d.getHeight()) / 1.0f), Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);
    canvas.scale(1.0f / 1.0f, 1.0f / 1.0f);
    Paint paint = new Paint();
    paint.setFlags(2);
    canvas.drawBitmap(bkg, 0.0f, 0.0f, paint);
    overlay = FastBlur.doBlur(overlay, (int) 100.0f, true);
    if (LetvUtils.getSDKVersion() >= 16) {
        view.setBackground(new BitmapDrawable(context.getResources(), overlay));
    } else {
        view.setBackgroundDrawable(new BitmapDrawable(context.getResources(), overlay));
    }
}
 
Example #4
Source File: a.java    From letv with Apache License 2.0 6 votes vote down vote up
private static final boolean b(String str, int i, int i2) {
    if (TextUtils.isEmpty(str)) {
        return false;
    }
    Options options = new Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(str, options);
    int i3 = options.outWidth;
    int i4 = options.outHeight;
    if (options.mCancel || options.outWidth == -1 || options.outHeight == -1) {
        return false;
    }
    int i5 = i3 > i4 ? i3 : i4;
    if (i3 >= i4) {
        i3 = i4;
    }
    f.b("AsynScaleCompressImage", "longSide=" + i5 + "shortSide=" + i3);
    options.inPreferredConfig = Config.RGB_565;
    if (i5 > i2 || i3 > i) {
        return true;
    }
    return false;
}
 
Example #5
Source File: DrawMosaicView.java    From PhotoEdit with Apache License 2.0 6 votes vote down vote up
/**
 * 返回马赛克最终结果
 * 
 * @return 马赛克最终结果
 */
public Bitmap getMosaicBitmap()
{
	if (bmMosaicLayer == null)
	{
		return null;
	}

	Bitmap bitmap = Bitmap.createBitmap(mImageWidth, mImageHeight,
			Config.ARGB_8888);
	Canvas canvas = new Canvas(bitmap);
	canvas.drawBitmap(bmBaseLayer, 0, 0, null);
	canvas.drawBitmap(bmMosaicLayer, 0, 0, null);
	canvas.save();
	return bitmap;
}
 
Example #6
Source File: PhotoUtils.java    From barterli_android with Apache License 2.0 6 votes vote down vote up
private static Bitmap compressManageAspect(final int height,
                final int width, final Bitmap originalImage) {
    final Bitmap compressedBitmap = Bitmap
                    .createBitmap(width, height, Config.ARGB_8888);
    final float originalWidth = originalImage.getWidth(), originalHeight = originalImage
                    .getHeight();
    final Canvas canvas = new Canvas(compressedBitmap);
    final float scale = width / originalWidth;
    final float xTranslation = 0.0f, yTranslation = (height - (originalHeight * scale)) / 2.0f;
    final Matrix transformation = new Matrix();
    transformation.postTranslate(xTranslation, yTranslation);
    transformation.preScale(scale, scale);
    final Paint paint = new Paint();
    paint.setFilterBitmap(true);
    canvas.drawBitmap(originalImage, transformation, paint);
    return compressedBitmap;
}
 
Example #7
Source File: PicassoRoundTransform.java    From CircularImageView with MIT License 6 votes vote down vote up
@Override
public Bitmap transform(final Bitmap source)
{
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

    Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    canvas.drawRoundRect(new RectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint);

    if (source != output) {
        source.recycle();
    }

    return output;
}
 
Example #8
Source File: CircularRevealHelper.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public void buildCircularRevealCache() {
  if (STRATEGY == BITMAP_SHADER) {
    buildingCircularRevealCache = true;
    hasCircularRevealCache = false;

    view.buildDrawingCache();
    Bitmap bitmap = view.getDrawingCache();

    if (bitmap == null && view.getWidth() != 0 && view.getHeight() != 0) {
      bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888);
      Canvas canvas = new Canvas(bitmap);
      view.draw(canvas);
    }

    if (bitmap != null) {
      revealPaint.setShader(new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP));
    }

    buildingCircularRevealCache = false;
    hasCircularRevealCache = true;
  }
}
 
Example #9
Source File: RawFileDecoder.java    From glide-support with The Unlicense 6 votes vote down vote up
@Override public @Nullable Resource<Bitmap> decode(File file, int w, int h, Options options) throws IOException {
	ByteBuffer buffer = ByteBuffer.allocate(w * h * 4);
	FileInputStream stream = new FileInputStream(file);
	try {
		stream.getChannel().read(buffer);
	} finally {
		stream.close();
	}
	Bitmap result = Bitmap.createBitmap(w, h, Config.ARGB_8888);
	try {
		buffer.rewind();
		result.copyPixelsFromBuffer(buffer);
		return BitmapResource.obtain(result, pool);
	} catch (RuntimeException ex) {
		result.recycle();
		throw ex;
	}
}
 
Example #10
Source File: StickerImageView.java    From PLDroidShortVideo with Apache License 2.0 6 votes vote down vote up
/**
 * 从 Drawable 中获取 Bitmap 对象
 */
private Bitmap drawable2Bitmap(Drawable drawable) {
    try {
        if (drawable == null) {
            return null;
        }
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
        int intrinsicWidth = drawable.getIntrinsicWidth();
        int intrinsicHeight = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth <= 0 ? DEFAULT_OTHER_DRAWABLE_WIDTH : intrinsicWidth, intrinsicHeight <= 0 ? DEFAULT_OTHER_DRAWABLE_HEIGHT : intrinsicHeight, Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    } catch (OutOfMemoryError e) {
        return null;
    }
}
 
Example #11
Source File: XBitmapUtils.java    From XFrame with Apache License 2.0 6 votes vote down vote up
/**
 * 生成水印图片 水印在右下角
 *
 * @param src       the bitmap object you want proecss
 * @param watermark the water mark above the src
 * @return return a bitmap object ,if paramter's length is 0,return null
 */
public static Bitmap createWatermarkBitmap(Bitmap src, Bitmap watermark) {
    if (src == null) {
        return null;
    }

    int w = src.getWidth();
    int h = src.getHeight();
    int ww = watermark.getWidth();
    int wh = watermark.getHeight();
    // create the new blank bitmap
    Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
    Canvas cv = new Canvas(newb);
    // draw src into
    cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src
    // draw watermark into
    cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印
    // save all clip
    cv.save(Canvas.ALL_SAVE_FLAG);// 保存
    // store
    cv.restore();// 存储
    return newb;
}
 
Example #12
Source File: ImageRequestTest.java    From volley with Apache License 2.0 6 votes vote down vote up
private void verifyResize(
        NetworkResponse networkResponse,
        int maxWidth,
        int maxHeight,
        ScaleType scaleType,
        int expectedWidth,
        int expectedHeight) {
    ImageRequest request =
            new ImageRequest("", null, maxWidth, maxHeight, scaleType, Config.RGB_565, null);
    Response<Bitmap> response = request.parseNetworkResponse(networkResponse);
    assertNotNull(response);
    assertTrue(response.isSuccess());
    Bitmap bitmap = response.result;
    assertNotNull(bitmap);
    assertEquals(expectedWidth, bitmap.getWidth());
    assertEquals(expectedHeight, bitmap.getHeight());
}
 
Example #13
Source File: ImageUtils_Deprecated.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

        if (null == bitmap) {
            return bitmap;
        }

        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 = 10;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }
 
Example #14
Source File: XBitmapUtils.java    From XFrame with Apache License 2.0 6 votes vote down vote up
/**
 * 获得圆角的Bitmap
 *
 * @param bitmap  源Bitmap
 * @param roundPx 圆角大小
 * @return 期望Bitmap
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

    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);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}
 
Example #15
Source File: GameView.java    From CatVision-io-SDK-Android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Bitmap getResBitmap(int bmpResId) {
	Options opts = new Options();
	opts.inDither = false;

	Resources res = getResources();
	Bitmap bmp = BitmapFactory.decodeResource(res, bmpResId, opts);

	if (bmp == null && isInEditMode()) {
		// BitmapFactory.decodeResource doesn't work from the rendering
		// library in Eclipse's Graphical Layout Editor. Use this workaround instead.

		Drawable d = res.getDrawable(bmpResId);
		int w = d.getIntrinsicWidth();
		int h = d.getIntrinsicHeight();
		bmp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
		Canvas c = new Canvas(bmp);
		d.setBounds(0, 0, w - 1, h - 1);
		d.draw(c);
	}

	return bmp;
}
 
Example #16
Source File: PictureBitmapTextureAtlasSource.java    From 30-android-libraries-in-30-days with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap onLoadBitmap(final Config pBitmapConfig) {
	final Picture picture = this.mPicture;
	if(picture == null) {
		Debug.e("Failed loading Bitmap in " + this.getClass().getSimpleName() + ".");
		return null;
	}

	final Bitmap bitmap = Bitmap.createBitmap(this.mTextureWidth, this.mTextureHeight, pBitmapConfig);
	final Canvas canvas = new Canvas(bitmap);

	final float scaleX = (float)this.mTextureWidth / this.mPicture.getWidth();
	final float scaleY = (float)this.mTextureHeight / this.mPicture.getHeight();
	canvas.scale(scaleX, scaleY, 0, 0);

	picture.draw(canvas);

	return bitmap;
}
 
Example #17
Source File: ImageUtils.java    From shinny-futures-android with GNU General Public License v3.0 6 votes vote down vote up
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    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 = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}
 
Example #18
Source File: RoundedDrawable.java    From ClipCircleHeadLikeQQ with Apache License 2.0 6 votes vote down vote up
public static Bitmap drawableToBitmap(Drawable drawable) {
  if (drawable instanceof BitmapDrawable) {
    return ((BitmapDrawable) drawable).getBitmap();
  }

  Bitmap bitmap;
  int width = Math.max(drawable.getIntrinsicWidth(), 2);
  int height = Math.max(drawable.getIntrinsicHeight(), 2);
  try {
    bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
  } catch (Exception e) {
    e.printStackTrace();
    Log.w(TAG, "Failed to create bitmap from drawable!");
    bitmap = null;
  }

  return bitmap;
}
 
Example #19
Source File: QRCodeGenerateActivity.java    From android-opensource-library-56 with Apache License 2.0 6 votes vote down vote up
private Bitmap bitMatrix2Bitmap(BitMatrix matrix) {
    int w = matrix.getWidth();
    int h = matrix.getHeight();
    int[] rawData = new int[w * h];
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            int color = Color.WHITE;
            if (matrix.get(i, j)) {
                color = Color.BLACK;
            }
            rawData[i + (j * w)] = color;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(w, h, Config.RGB_565);
    bitmap.setPixels(rawData, 0, w, 0, 0, w, h);
    return bitmap;
}
 
Example #20
Source File: ThemeListPreference.java    From droidddle with Apache License 2.0 6 votes vote down vote up
public static Bitmap getPreviewBitmap(float density, int color) {
    if (density == 0) {
        density = 1.5f;
    }
    int d = (int) (density * 31); // 30dip
    Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888);
    int w = bm.getWidth();
    int h = bm.getHeight();
    int c = color;
    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;
}
 
Example #21
Source File: ClassifierActivity.java    From dbclf with Apache License 2.0 6 votes vote down vote up
@Override
public void onPreviewSizeChosen(final Size size, final int rotation) {
    previewWidth = size.getWidth();
    previewHeight = size.getHeight();

    final int sensorOrientation = rotation - getScreenOrientation();

    rgbFrameBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
    croppedBitmap = Bitmap.createBitmap(INPUT_SIZE, INPUT_SIZE, Config.ARGB_8888);

    frameToCropTransform = ImageUtils.getTransformationMatrix(
            previewWidth, previewHeight,
            INPUT_SIZE, INPUT_SIZE,
            sensorOrientation, MAINTAIN_ASPECT);

    final Matrix cropToFrameTransform = new Matrix();
    frameToCropTransform.invert(cropToFrameTransform);
}
 
Example #22
Source File: RLImgUtil.java    From Roid-Library with Apache License 2.0 6 votes vote down vote up
/**
 * @param bitmap
 * @return
 */
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false);
    Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
    canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight()
            + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint);
    return bitmapWithReflection;
}
 
Example #23
Source File: BitmapUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 将彩色图转换为黑白图
 *
 * @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;
}
 
Example #24
Source File: TeXFormula.java    From AndroidMathKeyboard with Apache License 2.0 6 votes vote down vote up
public void createImage(Bitmap.CompressFormat format, int style,
		float size, String out, Integer bg, Integer fg, boolean transparency)
		throws IOException {
	TeXIcon icon = createTeXIcon(style, size);
	icon.setInsets(new Insets(1, 1, 1, 1));
	int w = icon.getIconWidth(), h = icon.getIconHeight();

	Bitmap image = Bitmap.createBitmap(w, h, Config.ARGB_8888);
	Canvas g2 = new Canvas(image);
	if (bg != null) {
		Paint st = new Paint();
		st.setStyle(Style.FILL_AND_STROKE);
		st.setColor(bg);
		g2.drawRect(0, 0, w, h, st);
	}

	icon.setForeground(fg == null ? Color.BLACK : fg);
	icon.paintIcon(g2, 0, 0);
	File file = new File(out);
	FileOutputStream imout = new FileOutputStream(file);
	image.compress(format, 90, imout);
	imout.flush();
	imout.close();
}
 
Example #25
Source File: RoundedDrawable.java    From Loop with Apache License 2.0 6 votes vote down vote up
public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    Bitmap bitmap;
    int width = Math.max(drawable.getIntrinsicWidth(), 2);
    int height = Math.max(drawable.getIntrinsicHeight(), 2);
    try {
        bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    } catch (Exception e) {
        e.printStackTrace();
        Log.w(TAG, "Failed to create bitmap from drawable!");
        bitmap = null;
    }

    return bitmap;
}
 
Example #26
Source File: UEImage.java    From Auie with GNU General Public License v2.0 6 votes vote down vote up
public UEImage transformRound(float radius){
	final int width = bitmap.getWidth();
	final int height = bitmap.getHeight();
	final int color = 0xff888888; 
	final Paint paint = new Paint();
	final Rect rect = new Rect(0, 0, width, height);
	final RectF rectF = new RectF(rect);
	if (radius == TRANSFORMROUND_CIRCLE) {
		radius = width > height ? width/2 : height/2;
	}
	Bitmap mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
	Canvas canvas = new Canvas(mBitmap);
	paint.setAntiAlias(true);
	paint.setColor(color);
	canvas.drawARGB(0, 0, 0, 0);
	canvas.drawRoundRect(rectF, radius, radius, paint);
	paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	canvas.drawBitmap(bitmap, rect, rect, paint);
	this.bitmap = mBitmap;
	return this;
}
 
Example #27
Source File: TeXFormula.java    From FlexibleRichTextView with Apache License 2.0 6 votes vote down vote up
public void createImage(Bitmap.CompressFormat format, int style,
		float size, String out, Integer bg, Integer fg, boolean transparency)
		throws IOException {
	TeXIcon icon = createTeXIcon(style, size);
	icon.setInsets(new Insets(1, 1, 1, 1));
	int w = icon.getIconWidth(), h = icon.getIconHeight();

	Bitmap image = Bitmap.createBitmap(w, h, Config.ARGB_8888);
	Canvas g2 = new Canvas(image);
	if (bg != null) {
		Paint st = new Paint();
		st.setStyle(Style.FILL_AND_STROKE);
		st.setColor(bg);
		g2.drawRect(0, 0, w, h, st);
	}

	icon.setForeground(fg == null ? Color.BLACK : fg);
	icon.paintIcon(g2, 0, 0);
	File file = new File(out);
	FileOutputStream imout = new FileOutputStream(file);
	image.compress(format, 90, imout);
	imout.flush();
	imout.close();
}
 
Example #28
Source File: BitmapUtils.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
private static Config getConfig(Bitmap bitmap) {
    Config config = bitmap.getConfig();
    if (config == null) {
        config = Config.ARGB_8888;
    }
    return config;
}
 
Example #29
Source File: BitmapUtils.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 压缩一个Bitmap
 */
public static Bitmap getScaleBitmap(Bitmap bitmap, float scale) {
    // 1,600px × 900px
    if (bitmap != null && bitmap.getWidth() > 1600 && bitmap.getHeight() > 900) {
        float sx = 1600 / (float) bitmap.getWidth();
        float sy = 900 / (float) bitmap.getHeight();
        Bitmap result = Bitmap.createBitmap(1600, 900, Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Matrix matrix = new Matrix();
        matrix.postScale(sx, sy);
        canvas.drawBitmap(bitmap, matrix, null);
        return result;
    }
    return bitmap;
}
 
Example #30
Source File: BitmapUtil.java    From lunzi with Apache License 2.0 5 votes vote down vote up
/**
 * @param resources 传入Resources
 * @param resid     资源ID
 * @param width     目标width
 * @param height    目标height
 * @return
 */
public static Bitmap getSmallBitmap(Resources resources, int resid, int width, int height) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(resources, resid, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);
    Log.i("Bitmap", "--" + options.inSampleSize);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, resid, options);
    if (bitmap == null) {
        return null;
    }
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    } finally {
        try {
            if (baos != null) baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}