Image 抠图,背景透明处理

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class ImageUtil {
	
	/**
     * 图片去白色的背景,并裁切
     *
     * @param image 图片
     * @param range 范围 1-255 越大 容错越高 去掉的背景越多
     * @return 图片
     * @throws Exception 异常
     */
    public static BufferedImage transferAlpha(BufferedImage bufferedImage, int range) throws Exception {
    	 BufferedImage sub = null;
    	try {
            ImageIcon imageIcon = new ImageIcon(bufferedImage);
            Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
            g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon
                    .getImageObserver());
            int alpha = 0;
            int minX = bufferedImage.getWidth();
            int minY = bufferedImage.getHeight();
            int maxX = 0;
            int maxY = 0;

            for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage
                    .getHeight(); j1++) {
                for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage
                        .getWidth(); j2++) {
                    int rgb = bufferedImage.getRGB(j2, j1);

                    int R = (rgb & 0xff0000) >> 16;
                    int G = (rgb & 0xff00) >> 8;
                    int B = (rgb & 0xff);
                    if (((255 - R) < range) && ((255 - G) < range) && ((255 - B) < range)) { //去除白色背景;
                        rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
                    } else {
                        minX = minX <= j2 ? minX : j2;
                        minY = minY <= j1 ? minY : j1;
                        maxX = maxX >= j2 ? maxX : j2;
                        maxY = maxY >= j1 ? maxY : j1;
                    }
                    bufferedImage.setRGB(j2, j1, rgb);
                }
            }
            int width = maxX - minX;
            int height = maxY - minY;
            sub = bufferedImage.getSubimage(minX, minY, width, height);
            ImageIO.write(sub, "png", new File("D:/new.png"));
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        return sub;
    }
}

  去掉白色背景,并剪切成透明背景。

二维码生成并抠图

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;


public String generateQRCode(String url) { checkNotNull(url, ErrorCode.ERROR_ILLEGAL_PARAMTER); log.error("二维码生成:url{}", url); int width = 300; int height = 300; String uploadFileToQiniu = null; Map<EncodeHintType, String> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = null; try { bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix); BufferedImage transferAlpha = ImageUtil.transferAlpha(bufferedImage, 1); if (transferAlpha == null) { throw new BusinessException(ErrorCode.ERROR_ALPHA);// 抠图失败 } ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(transferAlpha, "png", out); byte[] b = out.toByteArray(); } catch (Exception e) { throw new BusinessException(ErrorCode.ERROR_QRCODE); } return uploadFileToQiniu; }

  

  

原文地址:https://www.cnblogs.com/tietazhan/p/6110550.html