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

图片转为byte[]、String、图片之间的转换的更多相关文章

  1. Byte[]和BASE64之间的转换

    一. BASE64编码 把byte[]中的元素当做无符号八位整数转换成只含有64个基本字符的字符串,这些基本字符是: l 大写的A-Z l 小写的a-z l 数字0-9 l '+' 和 '/' l 空 ...

  2. Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

    package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...

  3. png格式图片转为svg格式图片

    png格式图片转为svg格式图片 (2012-08-30 16:24:00) 转载▼ 标签: 杂谈 分类: linux 在ubuntu下将png格式的图片转换成svg格式步骤如下:1.安装 inksc ...

  4. wchar_t char string wstring 之间的转换

    wchar_t char string wstring 之间的转换 转:http://blog.csdn.net/lbd2008/article/details/8333583 在处理中文时有时需要进 ...

  5. (转)CString,int,string,char*之间的转换

    CString,int,string,char*之间的转换http://www.cnblogs.com/greatverve/archive/2010/11/10/cstring-int-string ...

  6. MFC/C++/C中字符类型CString, int, string, char*之间的转换

    1 CString,int,string,char*之间的转换 string 转 CString CString.format("%s", string.c_str()); cha ...

  7. Java基本数据类型、包装类与String类之间的转换

    一.基本数据类型与包装类之间的转换: import org.junit.Test; public class MainTest { /** * 基本数据类型与包装类之间的转换 */ @Test pub ...

  8. VC CString,int,string,char*之间的转换

    CString转string : CString strMfc = "test"; std::string strStr; strStr = strMfc.GetBuffer(); ...

  9. CString,string,char*之间的转换(转)

    这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差.string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的:char*是从学习C语 ...

随机推荐

  1. Mvc全局过滤器与Action排除

    http://blog.csdn.net/shuaihj/article/details/53020428 如何一次性给所有action做登录验证过滤,如何排除不需要做登录验证的action? 1. ...

  2. [thinkphp] 无限极分类

    <?php /* * 无限极分类 类 */ header("Content-Type: text/html; charset=UTF-8"); Class Category ...

  3. POJ3468 A Simple Problem with Interger [树状数组,差分]

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  4. Android使用UncaughtExceptionHandler捕获全局异常

    Android系统的“程序异常退出”,给应用的用户体验造成不良影响.为了捕获应用运行时异常并给出友好提示,便可继承UncaughtExceptionHandler类来处理.通过Thread.setDe ...

  5. ExtJs之表单(form)

    --Form和Form Basic Extjs Form和Form Basic是两个东西,Form提供界面的展示,而Form Basic则提供数据的处理.验证等功能.每一个Form Panel在创建的 ...

  6. Bzoj4016/洛谷P2993 [FJOI2014] 最短路径树问题(最短路径问题+长链剖分/点分治)

    题面 Bzoj 洛谷 题解 首先把最短路径树建出来(用\(Dijkstra\),没试过\(SPFA\)\(\leftarrow\)它死了),然后问题就变成了一个关于深度的问题,可以用长链剖分做,所以我 ...

  7. Python开发基础-Day20继承实现原理、子类调用父类的方法、封装

    继承实现原理 python中的类可以同时继承多个父类,继承的顺序有两种:深度优先和广度优先. 一般来讲,经典类在多继承的情况下会按照深度优先的方式查找,新式类会按照广度优先的方式查找 示例解析: 没有 ...

  8. 杭电oj 1002

    #include <iostream> #include <algorithm> using namespace std; int nCases; ], n[]; ], b[] ...

  9. [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)

    Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0&l ...

  10. BZOJ 4028: [HEOI2015]公约数数列 分块

    4028: [HEOI2015]公约数数列 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4028 Description 设计一个数据结 ...