图片和字符串相互转换
http://blog.csdn.net/rmm0001/article/details/6692718
关闭
分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
- try{
- OutputStream o = response.getOutputStream();
- // 将图片转换成字符串
- File f = new File("f:\\Vista.png");
- FileInputStream fis = new FileInputStream( f );
- byte[] bytes = new byte[fis.available()];
- fis.read(bytes);
- fis.close();
- // 生成字符串
- String imgStr = byte2hex( bytes );
- System.out.println( imgStr);
- // 将字符串转换成二进制,用于显示图片
- // 将上面生成的图片格式字符串 imgStr,还原成图片显示
- byte[] imgByte = hex2byte( imgStr );
- InputStream in = new ByteArrayInputStream( imgByte );
- byte[] b = new byte[1024];
- int nRead = 0;
- while( ( nRead = in.read(b) ) != -1 ){
- o.write( b, 0, nRead );
- }
- o.flush();
- o.close();
- in.close();
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- }
- public static String byte2hex(byte[] b) // 二进制转字符串
- {
- StringBuffer sb = new StringBuffer();
- String stmp = "";
- for (int n = 0; n < b.length; n++) {
- stmp = Integer.toHexString(b[n] & 0XFF);
- if (stmp.length() == 1){
- sb.append("0" + stmp);
- }else{
- sb.append(stmp);
- }
- }
- return sb.toString();
- }
- public static byte[] hex2byte(String str) { // 字符串转二进制
- if (str == null)
- return null;
- str = str.trim();
- int len = str.length();
- if (len == 0 || len % 2 == 1)
- return null;
- byte[] b = new byte[len / 2];
- try {
- for (int i = 0; i < str.length(); i += 2) {
- b[i / 2] = (byte) Integer.decode("0X" + str.substring(i, i + 2)).intValue();
- }
- return b;
- } catch (Exception e) {
- return null;
- }
- }
- 顶
- 0
- 踩
- 0
我的同类文章
- •post 缓存2012-12-19
- •httpclient2012-12-13
- •android程序完全退出2012-04-19
- •自定义android循环拖动组件2012-04-11
- •二分法查找范例2010-07-22
- •URLConnection 使用流的问题2012-12-19
- •android - 自定义(组合)控件 + 自定义控件外观2012-12-07
- •android 网络2012-04-16
- •json和ajax简单例子2011-08-05
- •Java单例模式2010-04-22
- 猜你在找
查看评论
- 2楼 灿灿wly 2015-04-02 13:59发表 [回复]
- 将图片转换成字符串有需要加入其它的jar包么
- 1楼 csdn_850101 2012-10-24 17:13发表 [回复]
- 谢谢 文章写得好
您还没有登录,请[登录]或[注册]
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
核心技术类目
全部主题 Hadoop AWS 移动游戏 Java Android iOS Swift 智能硬件 Docker OpenStack VPN Spark ERPIE10 Eclipse CRM JavaScript 数据库 Ubuntu NFC WAP jQuery BI HTML5 Spring Apache .NET APIHTML SDK IIS Fedora XML LBS Unity Splashtop UML components Windows Mobile Rails QEMU KDECassandra CloudStack FTC coremail OPhone CouchBase 云计算 iOS6 Rackspace Web App SpringSide MaemoCompuware 大数据 aptech Perl Tornado Ruby Hibernate ThinkPHP HBase Pure Solr Angular Cloud FoundryRedis Scala Django Bootstrap
- 文章搜索
- 阅读排行
- android的ontouch事件(25381)
- 图片和字符串相互转换(9232)
- json和ajax简单例子(5103)
- Java 画线设置粗细、颜色(4438)
- jscrollpane布局管理(4327)
- android - 自定义(组合)控件 + 自定义控件外观(4138)
- post 缓存(2604)
- httpclient(2386)
- android的图片加载几种方法(2286)
- wcf简单的代码(1345)
- 评论排行
- 推荐文章
- 最新评论
- android的ontouch事件
未言再见丶: 很细致 谢谢
- 图片和字符串相互转换
灿灿wly: 将图片转换成字符串有需要加入其它的jar包么
- android程序完全退出
zxw136511485: 要是程序异常的话,广播就会发送失败,还是关不了的!
- android的ontouch事件
一位少侠: 说的很精细
- 图片和字符串相互转换
csdn_850101: 谢谢 文章写得好
- android的ontouch事件
chenjing_extensive: 我已经知道答案了
- android的ontouch事件
chenjing_extensive: 讲得非常细致。但我使用的时候遇到个问题。同时响应单击和双击,用onSingleTapUp和onDou...