硬章图片的透明化【抠图】java/android实现

背景

在实现图片硬章插入文档的时候,遇到了如下的问题。 给出的硬章的背景是白色的,并不是想要的透明色。这样有什么影响呢?这里贴一个图片可以进行观察。
在这里插入图片描述
你觉得对于硬章来说是透明色更接近现实情况呢,还是直接贴图好呢?

思路介绍

对于硬章图片,需要把他的白色背景,或者说是浅色背景扣调,也就是在白色或者接近白色的地方(255 255 255)给他的alpha 通道设置成0,对于有信息的地方(深色)alpha设置255,这样就是实现了硬章抠图。

避坑

如果是用opencv 实现的话也没有什么好写的,这里主要使用java 实现了抠图的过程,并在android中重写了该方法。
在java中是无法给colorModel 是RGB图片进行设置透明通道的,这里需要把图片变成ARGB的,这里可以通过Graphics2D进行转化

基本流程

  • 读取图片
  • 判断颜色模式,并统一到ARGB
  • 遍历像素
  • 浅色点设置透明通道为0

代码讲解

java实现

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @name SignImage
 * @date 2020/6/3 20:28
 */
public class SignImage {
    public static void main(String[] args) throws IOException {

        String filePath = "D://sign.jpg";
		//读取图片
        BufferedImage imageTmp = ImageIO.read(new File(filePath));
        int alphaLocal = imageTmp.getColorModel().getAlpha(0);
        //统一图片到argb
        if(imageTmp.getColorModel().getNumComponents() == 3){
            BufferedImage teemp = new BufferedImage(imageTmp.getWidth(),imageTmp.getHeight(),6);
            //6 是argb的意思
            //借助graphics 转argb
            Graphics2D graphics2D = DrawPictureUitl.getGraphics(teemp,
                    imageTmp.getWidth(),imageTmp.getHeight(),1);
            graphics2D.drawImage(imageTmp,0,0,null);
            imageTmp = teemp;
            graphics2D.dispose();
        }

        if ( alphaLocal == 255){
            int alpha = 0;
            //遍历像素。可以并行
            for (int j1 = imageTmp.getMinY(); j1 < imageTmp
                    .getHeight(); j1++) {
                for (int j2 = imageTmp.getMinX(); j2 < imageTmp
                        .getWidth(); j2++) {
                    int rgb = imageTmp.getRGB(j2, j1);
                    if (colorInRange(rgb)){
                        alpha = 0;
                    }
                    else{
                        alpha = 250;
                    }
                    rgb = (alpha << 24) | (rgb & 0x00ffffff);
                    imageTmp.setRGB(j2, j1, rgb);
                }
            }
        }
        //保存图片
        ImageIO.write(imageTmp, "png", new File("d://test.png"));
    }
    //检测颜色是否需要加透明通道
    public static boolean colorInRange(int color) {
        int red = (color & 0xff0000) >> 16;
        int green = (color & 0x00ff00) >> 8;
        int blue = (color & 0x0000ff);
        if (red >= 230 && green >= 230 && blue >= 230)
        {return true;}
        return false;
    }

}

对于android 来说,处理的是bitmap ,原理是一样的

//把白色转换成透明
    public static Bitmap getImageToChange(Bitmap mBitmap) {
        Bitmap createBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        if (mBitmap != null) {
            int mWidth = mBitmap.getWidth();
            int mHeight = mBitmap.getHeight();
            for (int i = 0; i < mHeight; i++) {
                for (int j = 0; j < mWidth; j++) {
                    int color = mBitmap.getPixel(j, i);
                    int g = Color.green(color);
                    int r = Color.red(color);
                    int b = Color.blue(color);
                    int a = Color.alpha(color);
                    if(g>=250&&r>=250&&b>=250){
                        a = 0;
                    }
                    color = Color.argb(a, r, g, b);
                    createBitmap.setPixel(j, i, color);
                }
            }
        }
        return createBitmap;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章