org.phoenix.imgreader.helper.ImageBase64Encoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of phoenix_imgreader Show documentation
Show all versions of phoenix_imgreader Show documentation
phoenixframework自动化平台用于图像文字识别的一个模块
package org.phoenix.imgreader.helper;
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 java.net.URL;
import java.net.URLConnection;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 网络图片与本地图片,通过base64编码相互转换
* @author mengfeiyang
*
*/
public class ImageBase64Encoder {
private static BASE64Encoder encoder = new BASE64Encoder();
private static BASE64Decoder decoder = new BASE64Decoder();
/**
* 根据图片的URL,将网络中的图片转换成base64编码
* @param imgFilePath
* @return
*/
public static String getUrlImgBase64Code(String url){
byte[] data = null;
try {
URL u = new URL(url);
URLConnection urlConn = u.openConnection();
InputStream in = urlConn.getInputStream();
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return encoder.encode(data);
}
/**
* 将本地的图片转换成base64编码
* @param imgFilePath
* @return
*/
public static String getLocalImgBase64Code(String imgFilePath){
byte[] data = null;
try{
InputStream in = new FileInputStream(new File(imgFilePath));
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e){
e.printStackTrace();
}
return encoder.encode(data);
}
/**
* 将base64编码后的字符串转成图片
* @param imgStr
* @param imgFilePath
* @return
*/
public static boolean decoderImage(String imgBase64Code, String imgFilePath){
if(imgBase64Code == null) return false;
try{
byte[] bytes = decoder.decodeBuffer(imgBase64Code);
for(int i = 0;i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy