直播源码搭建教程,如何通过图片获取指定大小的bitmap

直播源码搭建教程,如何通过图片获取指定大小的bitmap
一般通过图片获取其Bitmap的方法如下

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.head);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
System.out.println("Bitmap weight = "+width+" Bitmap height "+height);
Bitmap bitmap1 = Bitmap.createBitmap(bitmap, width/3, height/4, bitmap.getWidth() /3, bitmap.getHeight() /2);
imageView.setImageBitmap(bitmap1);

但是以上方法只是’裁剪’,如果只是想要固定大小的而不裁剪呢?

/**
 * Creates a new bitmap, scaled from an existing bitmap, when possible. If the
 * specified width and height are the same as the current width and height of
 * the source bitmap, the source bitmap is returned and no new bitmap is
 * created.
 *
 * @param src       The source bitmap.
 * @param dstWidth  The new bitmap's desired width.
 * @param dstHeight The new bitmap's desired height.
 * @param filter    Whether or not bilinear filtering should be used when scaling the
 *                  bitmap. If this is true then bilinear filtering will be used when
 *                  scaling which has better image quality at the cost of worse performance.
 *                  If this is false then nearest-neighbor scaling is used instead which
 *                  will have worse image quality but is faster. Recommended default
 *                  is to set filter to 'true' as the cost of bilinear filtering is
 *                  typically minimal and the improved image quality is significant.
 * @return The new scaled bitmap or the source bitmap if no scaling is required.
 * @throws IllegalArgumentException if width is <= 0, or height is <= 0
 */
public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight,
        boolean filter) {
    Matrix m = new Matrix();

    final int width = src.getWidth();
    final int height = src.getHeight();
    if (width != dstWidth || height != dstHeight) {
        final float sx = dstWidth / (float) width;
        final float sy = dstHeight / (float) height;
        m.setScale(sx, sy);
    }
    return Bitmap.createBitmap(src, 0, 0, width, height, m, filter);
}

以上就是直播源码搭建教程,如何通过图片获取指定大小的bitmap, 更多内容欢迎关注之后的文章

posted @ 2021-07-13 14:32  云豹科技-苏凌霄  阅读(88)  评论(0编辑  收藏  举报