android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte

 
  1. package com.bingo.util;
  2. import java.io.BufferedOutputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.Matrix;
  11. public class ImageDispose {
  12. /**
  13. * @param 将图片内容解析成字节数组
  14. * @param inStream
  15. * @return byte[]
  16. * @throws Exception
  17. */
  18. public static byte[] readStream(InputStream inStream) throws Exception {
  19. byte[] buffer = new byte[1024];
  20. int len = -1;
  21. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  22. while ((len = inStream.read(buffer)) != -1) {
  23. outStream.write(buffer, 0, len);
  24. }
  25. byte[] data = outStream.toByteArray();
  26. outStream.close();
  27. inStream.close();
  28. return data;
  29. }
  30. /**
  31. * @param 将字节数组转换为ImageView可调用的Bitmap对象
  32. * @param bytes
  33. * @param opts
  34. * @return Bitmap
  35. */
  36. public static Bitmap getPicFromBytes(byte[] bytes,
  37. BitmapFactory.Options opts) {
  38. if (bytes != null)
  39. if (opts != null)
  40. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
  41. opts);
  42. else
  43. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  44. return null;
  45. }
  46. /**
  47. * @param 图片缩放
  48. * @param bitmap 对象
  49. * @param w 要缩放的宽度
  50. * @param h 要缩放的高度
  51. * @return newBmp 新 Bitmap对象
  52. */
  53. public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){
  54. int width = bitmap.getWidth();
  55. int height = bitmap.getHeight();
  56. Matrix matrix = new Matrix();
  57. float scaleWidth = ((float) w / width);
  58. float scaleHeight = ((float) h / height);
  59. matrix.postScale(scaleWidth, scaleHeight);
  60. Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
  61. matrix, true);
  62. return newBmp;
  63. }
  64. /**
  65. * 把Bitmap转Byte
  66. * @Author HEH
  67. * @EditTime 2010-07-19 上午11:45:56
  68. */
  69. public static byte[] Bitmap2Bytes(Bitmap bm){
  70. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  71. bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
  72. return baos.toByteArray();
  73. }
  74. /**
  75. * 把字节数组保存为一个文件
  76. * @Author HEH
  77. * @EditTime 2010-07-19 上午11:45:56
  78. */
  79. public static File getFileFromBytes(byte[] b, String outputFile) {
  80. BufferedOutputStream stream = null;
  81. File file = null;
  82. try {
  83. file = new File(outputFile);
  84. FileOutputStream fstream = new FileOutputStream(file);
  85. stream = new BufferedOutputStream(fstream);
  86. stream.write(b);
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. } finally {
  90. if (stream != null) {
  91. try {
  92. stream.close();
  93. } catch (IOException e1) {
  94. e1.printStackTrace();
  95. }
  96. }
  97. }
  98. return file;
  99. }
  100. }

转自:http://blog.csdn.net/z104207/article/details/6634774

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值