base64图片去白底
生活随笔
收集整理的這篇文章主要介紹了
base64图片去白底
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
圖片去白底
// 去白邊調用 String base64 = getBase64(ImageUtil.images(Base64ToImage(xp)));/*** base64生成圖片文件** @param base64Data* @return*/private static File Base64ToImage(String base64Data) {BASE64Decoder decoder = new BASE64Decoder();File temp = null;try {//Base64解碼byte[] b = decoder.decodeBuffer(base64Data);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {//調整異常數據b[i] += 256;}}//生成jpeg圖片temp = File.createTempFile("temp", ".png");OutputStream out = new FileOutputStream(temp);out.write(b);out.flush();out.close();return temp;} catch (Exception e) {return temp;}}//將生成的證照圖片轉成base格式private String getBase64(BufferedImage cardBase64img) throws IOException {ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流ImageIO.write(cardBase64img, "png", baos);//寫入流中byte[] bytes = baos.toByteArray();//轉換成字節//String png_base64 = new BASE64Encoder().encode(bytes.toString().getBytes("UTF-8"));BASE64Encoder encoder = new BASE64Encoder();String png_base64 = encoder.encodeBuffer(bytes);//轉換成base64串png_base64.getBytes("UTF-8");png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//刪除 \r\nreturn png_base64;} import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.InputStream;public class ImageUtil {public static BufferedImage images(File file) {// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//File file = new File("D:\\08\\11.png");InputStream is;BufferedImage bufferedImage = null;try {is = new FileInputStream(file);// 如果是MultipartFile類型,那么自身也有轉換成流的方法:is = file.getInputStream();BufferedImage bi = ImageIO.read(is);Image image = (Image) bi;ImageIcon imageIcon = new ImageIcon(image);bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),BufferedImage.TYPE_4BYTE_ABGR);Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());int alpha = 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) < 30) && ((255 - G) < 30) && ((255 - B) < 30)) {rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);}bufferedImage.setRGB(j2, j1, rgb);}}g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());return bufferedImage;//ImageIO.write(bufferedImage, "png", new File("D:\\13.png"));// 直接輸出文件} catch (Exception e) {e.printStackTrace();}return bufferedImage;}}?
總結
以上是生活随笔為你收集整理的base64图片去白底的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 虚拟邮箱地址-可随意接收邮箱验证码——网
- 下一篇: 圣诞节计算机音乐,圣诞节音乐