图片转为byte[]、String、图片之间的转换
2024-08-27 23:02:44
package com.horizon.action; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream; public class Hello {
// 图片到byte数组
public byte[] image2byte(String path) {
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[];
int numBytesRead = ;
while ((numBytesRead = input.read(buf)) != -) {
output.write(buf, , numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
} catch (FileNotFoundException ex1) {
ex1.printStackTrace();
} catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
} // byte数组到图片
public void byte2image(byte[] data, String path) {
if (data.length < || path.equals(""))
return;
try {
FileImageOutputStream imageOutput = new FileImageOutputStream(
new File(path));
imageOutput.write(data, , data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in "
+ path);
} catch (Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
} /**
* Convert byte[] to hex
* string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。
*
* @param src
* byte[] data
* @return hex string
*/
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= ) {
return null;
}
for (int i = ; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < ) {
stringBuilder.append();
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
} /**
* Convert hex string to byte[]
*
* @param hexString
* the hex string
* @return byte[]
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / ;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = ; i < length; i++) {
int pos = i * ;
d[i] = (byte) (charToByte(hexChars[pos]) << | charToByte(hexChars[pos + ]));
}
return d;
} /**
* Convert char to byte
*
* @param c
* char
* @return byte
*/
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
} @SuppressWarnings("static-access")
public static void main(String[] args) {
String fromPath = "C:/Users/Administrator/123.jpg";
String toPath = "C:/Users/Administrator/456.jpg";
Hello hello = new Hello();
byte[] bytes = hello.image2byte(fromPath);
String hexString = hello.bytesToHexString(bytes);
byte[] bytes2 = hello.hexStringToBytes(hexString);
hello.byte2image(bytes2, toPath);
}
}
最新文章
- ad
- Kakfa分布式集群搭建
- CentOS 5.5 下安装Countly Web Server过程记录
- Bash简单介绍
- PHP~foreach遍历名单数组~有必要多次观看练习
- 出现并解决Navicat 报错:1130-host ... is not allowed to connect to this MySql server,MySQL
- hadoop的thriftserver配置
- 如何查看VS中预设的路径变量
- KMP算法(转)
- How to:如何在调用外部文件时调试文件路径(常见于使用LaunchAppAndWait和LaunchApp函数)
- 第35篇 IIS执行原理
- 进行app性能和安全性测试的重要性
- centos/linux下的使得maven/tomcat能在普通用户是使用
- 题解 P3871 【[TJOI2010]中位数】
- [PHP] 排序和查找算法
- 鼠标悬浮控制元素隐藏与显示 - css中鼠标的hover状态
- <;input type=";date";>;设置默认当前日期
- SpringBoot入门篇--使用IDEA创建一个SpringBoot项目
- springcloud(九) springboot Actuator + admin 监控
- 最长无重复字符的子串 &#183; Longest Substring Without Repeating Characters