图片转换处理主要是用在传输过程中需要。http传输图片,表单方式就不介绍了。如果采用的是通过编码的形式来传输图片,那么发送和接受都需要对图片和编码进行转换处理。这里也整理了一下开发中用过的方法。
注意:图片转换成编码后,编码占用内存会比原来图片更大,甚至翻数。所以用大图片需要谨慎,不然极容易发生内存溢出。
1.将图片转成base64编码字符串
/**
* @Description: 根据图片地址转换为base64编码字符串
* @Author:
* @CreateTime:
* @return
*/
public static String getImageStr(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
2.将图片转成16进制编码字符串
/**
* @Description: 根据图片地址转换为16编码字符串
* @Author:
* @CreateTime:
* @return String
*/
public static String getImage16Str(String imgFile) {
try {
StringBuffer sb = new StringBuffer();
FileInputStream fis = new FileInputStream(imgFile);
BufferedInputStream bis = new BufferedInputStream(fis);
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len = 0;
while ((len = fis.read(buff)) != -1) {
bos.write(buff, 0, len);
}
byte[] result = bos.toByteArray();
String str = byte2hex(result);
return str;
} catch (IOException e) {
}
return null;
}
3.base64字符串转化成图片
/**
* base64字符串转化成图片
* @param imgStr
* @return
*/
public static boolean GenerateImage(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
String imgFilePath = "E:/test22.jpg";// 新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
4.字节转换生成图片
/**
* 字节生成图片
* */
public static void getFile(byte[] bfile, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath + "\\" + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
附:字节转16进制
/*
* 实现字节数组向十六进制的转换的方法二
*/
public static String byte2hex(byte[] b) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char[] out = new char[b.length * 2];
for (int i = 0; i < b.length; i++) {
byte c = b[i];
out[i * 2] = Digit[(c >>> 4) & 0X0F];
out[i * 2 + 1] = Digit[c & 0X0F];
}
return new String(out);
}
附:16进制转字节
/**
* 16进制转字节
* @param s
* @return
*/
public static byte[] hex2byte(String s) {
byte[] src = s.toLowerCase().getBytes();
byte[] ret = new byte[src.length / 2];
for (int i = 0; i < src.length; i += 2) {
byte hi = src[i];
byte low = src[i + 1];
hi = (byte) ((hi >= 'a' && hi <= 'f') ? 0x0a + (hi - 'a') : hi - '0');
low = (byte) ((low >= 'a' && low <= 'f') ? 0x0a + (low - 'a') : low - '0');
ret[i / 2] = (byte) (hi << 4 | low);
}
return ret;
}