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

图片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;
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?