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);
}
}

最新文章

  1. ad
  2. Kakfa分布式集群搭建
  3. CentOS 5.5 下安装Countly Web Server过程记录
  4. Bash简单介绍
  5. PHP~foreach遍历名单数组~有必要多次观看练习
  6. 出现并解决Navicat 报错:1130-host ... is not allowed to connect to this MySql server,MySQL
  7. hadoop的thriftserver配置
  8. 如何查看VS中预设的路径变量
  9. KMP算法(转)
  10. How to:如何在调用外部文件时调试文件路径(常见于使用LaunchAppAndWait和LaunchApp函数)
  11. 第35篇 IIS执行原理
  12. 进行app性能和安全性测试的重要性
  13. centos/linux下的使得maven/tomcat能在普通用户是使用
  14. 题解 P3871 【[TJOI2010]中位数】
  15. [PHP] 排序和查找算法
  16. 鼠标悬浮控制元素隐藏与显示 - css中鼠标的hover状态
  17. &lt;input type=&quot;date&quot;&gt;设置默认当前日期
  18. SpringBoot入门篇--使用IDEA创建一个SpringBoot项目
  19. springcloud(九) springboot Actuator + admin 监控
  20. 最长无重复字符的子串 &#183; Longest Substring Without Repeating Characters

热门文章

  1. 关于自建yum源拾遗
  2. 查找文件which locate find
  3. SpringBoot学习:整合Mybatis,使用HikariCP超高性能数据源
  4. LD_PRELOAD的妙用,让python支持自己编译的Sqlite
  5. Java中的冒泡排序(减少比较次数)
  6. Sqli-labs less 7
  7. Linux系统的目录结构及各目录作用
  8. hdu 5592 ZYB&#39;s Premutation (权值线段树)
  9. 【字符串哈希】【哈希表】Aizu - 1370 - Hidden Anagrams
  10. python基础之封装与绑定方法