Java读取图片
发布日期:2021-05-10 02:39:47 浏览次数:15 分类:精选文章

本文共 4200 字,大约阅读时间需要 14 分钟。

���������������������������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������

public class ImageConvertUtil {
/**
* ���������������������������������������
* @param imgStr ������������������Base64���������������
* @param filename ���������������������������������"D:/Systems/"���
* @return ������������������
*/
public static boolean generateImage(String imgStr, String filename) {
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("D:/Systems/" + filename);
out.write(b);
out.flush();
out.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* ���������������������������������������
* @param filePath ������������
* @return Base64���������������
*/
public static String getImageStr(String filePath) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(filePath);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// ���������������Base64���������
return new BASE64Encoder().encode(data);
}
/**
* ������������
* @param args ������������������
*/
public static void main(String[] args) {
// ������������������������
String imageStr = getImageStr("D:/001.jpg");
System.out.println(imageStr);
// ������������������������
boolean result = generateImage(imageStr, "001.jpg");
System.out.println(result);
}
}

���������������������������

1. generateImage������

  • ������������Base64������������������������������������������������������
  • ���������������
  • ������������������������������null������������������������������
  • ������BASE64Decoder������Base64���������������������������������
  • ���������������������������������������������������������
  • ������output stream������������������������������������������������������������
  • ���������������������

2. getImageStr������

  • ������������������������������������������������Base64������������
  • ���������������
  • ���������������������������������
  • ������������������������byte���������
  • ������������������������������byte������������
  • ������BASE64Encoder���byte���������������Base64������������
  • ���������������Base64������������

3. main������������

  • ������������������������������������������������������
  • ���������������
  • ������getImageStr���������"001.jpg"���������������Base64������������
  • ������generateImage���������Base64���������������������������������
  • ���������������������

���������������������

  • BASE64Decoder/Encoder���������������Base64������������������
  • FileInputStream/OutputStream������������������������������������

������������

  • ���������������������������������������������������������������������������������������������������
  • ������������������������try-catch������������������������������������������������������������������������������������������
  • ��������������������������������������������������������������������������������������������������� bufferedstream ������������

������������������

���������������������������
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=
���������������������������
true

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:java获取IP
下一篇:org.apache.http.client.ClientProtocolException: null

发表评论

最新留言

很好
[***.229.124.182]2025年04月21日 12时31分04秒