Commit 15930c13 authored by jiaorz's avatar jiaorz

Merge branch 'master-captcha-branch' into dev

parents 16f9f6b9 ea76b244
package com.xxfc.platform.universal.utils;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class base64Change {
/**
* @param imgStr base64编码字符串
* @param path 图片路径-具体到文件
*/
public static boolean generateImage(String imgStr, String path) {
if (imgStr == null)
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// 解密
byte[] b = decoder.decodeBuffer(imgStr);
// 处理数据
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
OutputStream out = new FileOutputStream(path);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
public static void main(String[] args) {
String path = "d:\\1.jpg";
String imgFile = "D:\\Downloads\\light_yan-captcha-master\\captcha\\images\\pic-click\\bg5.png";//待处理的图片
String imgpath = GetImageStr(imgFile);
System.out.println(generateImage(imgpath, path));
}
//图片转化成base64字符串
public static String GetImageStr(String imgFile)
{//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
// 地址也有写成"F:/deskBG/86619-107.jpg"形式的
try {
return encodeToString(imgFile);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String encodeToString(String imagePath) throws IOException {
String type = StringUtils.substring(imagePath, imagePath.lastIndexOf(".") + 1);
BufferedImage image = ImageIO.read(new File(imagePath));
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
imageString = encoder.encode(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
}
\ No newline at end of file
...@@ -70,13 +70,13 @@ public abstract class AbstractCaptcha { ...@@ -70,13 +70,13 @@ public abstract class AbstractCaptcha {
protected String getImageToBase64Str (BufferedImage templateImage){ protected String getImageToBase64Str (BufferedImage templateImage){
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { try {
ImageIO.write(templateImage, "jpg", baos); ImageIO.write(templateImage, "png", baos);
} catch (IOException e) { } catch (IOException e) {
throw new CaptchaException("ImageIO.write is error", e); throw new CaptchaException("ImageIO.write is error", e);
} }
byte[] bytes = baos.toByteArray(); byte[] bytes = baos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder(); BASE64Encoder encoder = new BASE64Encoder();
return encoder.encodeBuffer(bytes).trim(); return encoder.encodeBuffer(bytes).trim().replaceAll("\n", "").replaceAll("\r", "");
} }
public String getJigsawUrlOrPath() { public String getJigsawUrlOrPath() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment