直播源码搭建教程,如何通过图片获取指定大小的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 @   云豹科技-苏凌霄  阅读(93)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示