This commit is contained in:
sjm
2023-12-25 02:58:26 +08:00
parent 9161f7357e
commit 8f8bbf22ac
2 changed files with 110 additions and 24 deletions

View File

@@ -20,6 +20,7 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
@Component
@Service
@@ -431,7 +432,56 @@ public class QRCodeUtil {
return (serverPath +"/" + fileName);
}
// 对字节数组字符串进行Base64解码并生成图片
//imgFilePath 待保存的本地路径
public static String GenerateImage(String base64Str, String imgFilePath) {
if (base64Str == null) // 图像数据为空
return null;
Base64.Decoder decoder = Base64.getDecoder();
try {
// Base64解码
byte[] bytes = decoder.decode(base64Str);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
//设置图片名称,这个随意
String date = new Random().nextInt(99999999)+".jpg";
String fileName = date ;
// 生成jpg图片
OutputStream out = new FileOutputStream(imgFilePath +"/" + fileName);
out.write(bytes);
out.flush();
out.close();
return (imgFilePath +"/" + fileName);
} catch (Exception e) {
return null;
}
}
public static boolean isBase64(String url){
String reg = "[a-zA-z]+://[^\\s]*";
boolean isMatch = Pattern.matches(reg, url);
if(isMatch){
return isMatch;
}
return isMatch;
}
public static String delete_profix(String base64){
String delete = "data:image/jpg;base64,";
String Base64 = base64;
return(base64.replace(delete,""));
}
public static void main(String[] args) throws Exception {
// String text = "https://www.baidu.com"; //这里设置自定义网站url
// String logoPath ="src/main/resources/static/logo/NAV.png";
// String destPath = "src/main/resources/static/qr";