溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

前端js 生成的base64位圖片怎樣在后端 java

發(fā)布時間:2021-10-19 17:06:57 來源:億速云 閱讀:135 作者:柒染 欄目:大數(shù)據(jù)

前端js 生成的base64位圖片怎樣在后端 java,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

// 1.需要引入的包

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

// 2. 用到的方法:

/**
     * 圖片轉(zhuǎn)BASE64
     * @param imagePath 路徑
     * @return
     */
    public static String imageChangeBase64(String imagePath){
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imagePath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
    
    /**
     * BASE轉(zhuǎn)圖片
     * @param baseStr  base64字符串
     * @param imagePath 生成的圖片
     * @return
     */
    public static boolean base64ChangeImage(String baseStr,String imagePath){
        if (baseStr == null){
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 解密
            byte[] b = decoder.decodeBuffer(baseStr);
            // 處理數(shù)據(jù)
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(imagePath);
            out.write(b);
             out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

// 3 解析測試的方法:

public static void test1(){

// 要生成的圖片的地址

 String pp = imageChangeBase64("E:\\58744.jpg");

// 打印生成的base64位置編碼
        System.out.println("pp:"+pp);

  String pp3=""; // 此處省略為前端返給你的base64圖片碼,

      base64ChangeImage(pp2.split(",")[1],"E:\\bb.jpg");
 

}

// 將圖片生成base64位的網(wǎng)絡(luò)地址:

http://imgbase64.duoshitong.com/

關(guān)于前端js 生成的base64位圖片怎樣在后端 java問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI