听相声 2018-03-27 13:23 采纳率: 33.3%
浏览 1621
已结题

图片TYPE_4BYTE_ABGR 转换成TYPE_3BYTE_BGR

最近做html5 中的video打开摄像头拍摄照片保存到本地之后,图片格式为TYPE_4BYTE_ABGR类型,但是图片在使用到时候需要转换成TYPE_3BYTE_BGR格式,在网上找了很多格式转换的,都没有解决

  • 写回答

1条回答 默认 最新

  • qq1521607368 2018-03-27 14:13
    关注

    This is the code that you need, you have to put the same color with 100% alpha in a new image with alpha channel.

    // bufferedImage is your image.
    if (bufferedImage.getType() == BufferedImage.TYPE_3BYTE_BGR) {
    BufferedImage bff = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
    for (int y = 0; y < bufferedImage.getHeight(); ++y) {
    for (int x = 0; x < bufferedImage.getWidth(); ++x) {
    int argb = bufferedImage.getRGB(x, y);
    bff.setRGB(x, y, argb & 0xFF000000); // same color alpha 100%
    }
    }
    return bff;
    } else {
    return bufferedImage;
    }

    if you want to put a transparent image on a white background you should indeed use a new BufferedImage with different type. The code to do so is this:

    // bufferedImage is your image.
    if (bufferedImage.getType() == BufferedImage.TYPE_4BYTE_ABGR) {
    for (int y = 0; y < bufferedImage.getHeight(); ++y) {
    for (int x = 0; x < bufferedImage.getWidth(); ++x) {
    int argb = bufferedImage.getRGB(x, y);
    if((argb & 0x00FFFFFF) == 0x00FFFFFF){ //if the pixel is transparent
    bufferedImage.setRGB(x, y, 0xFFFFFFFF); // white color.
    }
    }
    }
    return bufferedImage;
    }

    评论

报告相同问题?

悬赏问题

  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务安装失败