图片转为byte[]、String、图片之间的转换
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、图片之间的转换的更多相关文章
- Byte[]和BASE64之间的转换
一. BASE64编码 把byte[]中的元素当做无符号八位整数转换成只含有64个基本字符的字符串,这些基本字符是: l 大写的A-Z l 小写的a-z l 数字0-9 l '+' 和 '/' l 空 ...
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...
- png格式图片转为svg格式图片
png格式图片转为svg格式图片 (2012-08-30 16:24:00) 转载▼ 标签: 杂谈 分类: linux 在ubuntu下将png格式的图片转换成svg格式步骤如下:1.安装 inksc ...
- wchar_t char string wstring 之间的转换
wchar_t char string wstring 之间的转换 转:http://blog.csdn.net/lbd2008/article/details/8333583 在处理中文时有时需要进 ...
- (转)CString,int,string,char*之间的转换
CString,int,string,char*之间的转换http://www.cnblogs.com/greatverve/archive/2010/11/10/cstring-int-string ...
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
1 CString,int,string,char*之间的转换 string 转 CString CString.format("%s", string.c_str()); cha ...
- Java基本数据类型、包装类与String类之间的转换
一.基本数据类型与包装类之间的转换: import org.junit.Test; public class MainTest { /** * 基本数据类型与包装类之间的转换 */ @Test pub ...
- VC CString,int,string,char*之间的转换
CString转string : CString strMfc = "test"; std::string strStr; strStr = strMfc.GetBuffer(); ...
- CString,string,char*之间的转换(转)
这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差.string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的:char*是从学习C语 ...
随机推荐
- Excel Application对象应用
Application对象是Excel对象模型中最高层级的对象,代表Excel应用程序自身,也包含组成工作簿的许多部分,包括工作簿.工作表.单元格集合以及它们包含的数据. Application对象包 ...
- sublime插件总汇
本人用过多个编辑器(非IDE),发现sublime的样式非常帮,现在做个插件总汇 ChineseLocalzations 中文插件 Emmet ...
- python判断一个对象是可迭代?
1.介绍一下如何判断一个对象是可迭代的? 通过collections模块的Iterable类型判断: >>> from collections import Iterable > ...
- 洛谷P1880 [NOI1995] 石子合并 [DP,前缀和]
题目传送门 题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将N堆 ...
- scrapy抓取拉勾网职位信息(一)——scrapy初识及lagou爬虫项目建立
本次以scrapy抓取拉勾网职位信息作为scrapy学习的一个实战演练 python版本:3.7.1 框架:scrapy(pip直接安装可能会报错,如果是vc++环境不满足,建议直接安装一个visua ...
- Codeforces Round #196 (Div. 1) 题解
(CF唯一不好的地方就是时差……不过还好没去考,考的话就等着滚回Div. 2了……) A - Quiz 裸的贪心,不过要用矩阵乘法优化或者直接推通式然后快速幂.不过本傻叉做的时候脑子一片混乱,导致WA ...
- ccpc秦皇岛部分题解
A. 题意:就是有一个大桌子,环绕有顺势站1~m共m个座位,n个选手坐在部分位置上.然后如果有一个人a了一道题,却没有立刻发气球给他,他产生怒气值是发气球给他的时间减去a题时间.现在有一个机器人顺时针 ...
- [CODE FESTIVAL 2016]Encyclopedia of Permutations
题意:给定一个排列,其中有可能有一些未确定的数,求出所有可能的排列的排名之和 首先我们要知道怎么算一个给定排列的排名,设它为$p_{1\cdots n}$ 排名即为比它小的排列数$+1$,对于每一个比 ...
- [UOJ62]怎样跑得更快
以下用等号代替同余 这个式子是$\sum\limits_{j=1}^n(i,j)^{c-d}i^dj^dx_j=b_i$ 令$g(n)=\sum\limits_{e|n}\mu\left(\frac ...
- 【置换群】【枚举约数】hdu6038 Function
把b数组的所有置换群求出来,用数组记录一下每个大小所出现的次数. 然后求a的置换群,对每个置换群求能被其整除的b的置换群的大小总和(只有这些才能满足构造出一个f,且不自相矛盾),然后把它们全都乘起来就 ...