android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byt
2012-07-16 16:54
916 查看
import java.io.BufferedOutputStream;
04.import java.io.ByteArrayOutputStream;
05.import java.io.File;
06.import java.io.FileOutputStream;
07.import java.io.IOException;
08.import java.io.InputStream;
09.
10.import android.graphics.Bitmap;
11.import android.graphics.BitmapFactory;
12.import android.graphics.Matrix;
13.
14.public class ImageDispose {
15.
16.
17.
18. /**
19. * @param 将图片内容解析成字节数组
20. * @param inStream
21. * @return byte[]
22. * @throws Exception
23. */
24. public static byte[] readStream(InputStream inStream) throws Exception {
25. byte[] buffer = new byte[1024];
26. int len = -1;
27. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
28. while ((len = inStream.read(buffer)) != -1) {
29. outStream.write(buffer, 0, len);
30. }
31. byte[] data = outStream.toByteArray();
32. outStream.close();
33. inStream.close();
34. return data;
35.
36. }
37. /**
38. * @param 将字节数组转换为ImageView可调用的Bitmap对象
39. * @param bytes
40. * @param opts
41. * @return Bitmap
42. */
43. public static Bitmap getPicFromBytes(byte[] bytes,
44. BitmapFactory.Options opts) {
45. if (bytes != null)
46. if (opts != null)
47. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
48. opts);
49. else
50. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
51. return null;
52. }
53. /**
54. * @param 图片缩放
55. * @param bitmap 对象
56. * @param w 要缩放的宽度
57. * @param h 要缩放的高度
58. * @return newBmp 新 Bitmap对象
59. */
60. public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){
61. int width = bitmap.getWidth();
62. int height = bitmap.getHeight();
63. Matrix matrix = new Matrix();
64. float scaleWidth = ((float) w / width);
65. float scaleHeight = ((float) h / height);
66. matrix.postScale(scaleWidth, scaleHeight);
67. Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
68. matrix, true);
69. return newBmp;
70. }
71.
72. /**
73. * 把Bitmap转Byte
74. * @Author HEH
75. * @EditTime 2010-07-19 上午11:45:56
76. */
77. public static byte[] Bitmap2Bytes(Bitmap bm){
78. ByteArrayOutputStream baos = new ByteArrayOutputStream();
79. bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
80. return baos.toByteArray();
81. }
82. /**
83. * 把字节数组保存为一个文件
84. * @Author HEH
85. * @EditTime 2010-07-19 上午11:45:56
86. */
87. public static File getFileFromBytes(byte[] b, String outputFile) {
88. BufferedOutputStream stream = null;
89. File file = null;
90. try {
91. file = new File(outputFile);
92. FileOutputStream fstream = new FileOutputStream(file);
93. stream = new BufferedOutputStream(fstream);
94. stream.write(b);
95. } catch (Exception e) {
96. e.printStackTrace();
97. } finally {
98. if (stream != null) {
99. try {
100. stream.close();
101. } catch (IOException e1) {
102. e1.printStackTrace();
103. }
104. }
105. }
106. return file;
107. }
108.
109.}
04.import java.io.ByteArrayOutputStream;
05.import java.io.File;
06.import java.io.FileOutputStream;
07.import java.io.IOException;
08.import java.io.InputStream;
09.
10.import android.graphics.Bitmap;
11.import android.graphics.BitmapFactory;
12.import android.graphics.Matrix;
13.
14.public class ImageDispose {
15.
16.
17.
18. /**
19. * @param 将图片内容解析成字节数组
20. * @param inStream
21. * @return byte[]
22. * @throws Exception
23. */
24. public static byte[] readStream(InputStream inStream) throws Exception {
25. byte[] buffer = new byte[1024];
26. int len = -1;
27. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
28. while ((len = inStream.read(buffer)) != -1) {
29. outStream.write(buffer, 0, len);
30. }
31. byte[] data = outStream.toByteArray();
32. outStream.close();
33. inStream.close();
34. return data;
35.
36. }
37. /**
38. * @param 将字节数组转换为ImageView可调用的Bitmap对象
39. * @param bytes
40. * @param opts
41. * @return Bitmap
42. */
43. public static Bitmap getPicFromBytes(byte[] bytes,
44. BitmapFactory.Options opts) {
45. if (bytes != null)
46. if (opts != null)
47. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
48. opts);
49. else
50. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
51. return null;
52. }
53. /**
54. * @param 图片缩放
55. * @param bitmap 对象
56. * @param w 要缩放的宽度
57. * @param h 要缩放的高度
58. * @return newBmp 新 Bitmap对象
59. */
60. public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){
61. int width = bitmap.getWidth();
62. int height = bitmap.getHeight();
63. Matrix matrix = new Matrix();
64. float scaleWidth = ((float) w / width);
65. float scaleHeight = ((float) h / height);
66. matrix.postScale(scaleWidth, scaleHeight);
67. Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
68. matrix, true);
69. return newBmp;
70. }
71.
72. /**
73. * 把Bitmap转Byte
74. * @Author HEH
75. * @EditTime 2010-07-19 上午11:45:56
76. */
77. public static byte[] Bitmap2Bytes(Bitmap bm){
78. ByteArrayOutputStream baos = new ByteArrayOutputStream();
79. bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
80. return baos.toByteArray();
81. }
82. /**
83. * 把字节数组保存为一个文件
84. * @Author HEH
85. * @EditTime 2010-07-19 上午11:45:56
86. */
87. public static File getFileFromBytes(byte[] b, String outputFile) {
88. BufferedOutputStream stream = null;
89. File file = null;
90. try {
91. file = new File(outputFile);
92. FileOutputStream fstream = new FileOutputStream(file);
93. stream = new BufferedOutputStream(fstream);
94. stream.write(b);
95. } catch (Exception e) {
96. e.printStackTrace();
97. } finally {
98. if (stream != null) {
99. try {
100. stream.close();
101. } catch (IOException e1) {
102. e1.printStackTrace();
103. }
104. }
105. }
106. return file;
107. }
108.
109.}
相关文章推荐
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为Ima ...
- android 将图片内容解析成字节数组,将字节数组转换为ImageView
- Android 将布局文件转成图片(将View 转换成BitMap)
- android 将一个bitmap保存成图片文件
- android截屏:保存一个view的内容为图片并存放到SD卡
- Android BitmapFactory.Options 对图片进行缩放以符合ImageView大小
- android——获取ImageView上面显示的图片bitmap对象
- android——获取ImageView上面显示的图片bitmap对象
- android开发布局文件imageview 图片等比例缩放:
- android如何获取res文件夹下drawable文件夹下的图片资源文件转成一个BitMap对象
- Android 把bitmap转换成String,计算图片的缩放值,根据路径获得突破并压缩返回bitmap用于显示,获取保存 隐患检查的图片文件夹名称,获取保存图片的目录,根据路径删除图片,添加到图库
- android 怎么把网络图片生成一个Bitmap显示在ImageView中
- 动态放大缩小ImageView里的图片……运用Matrix对象来缩放图片文件
- Android学习之Bitmap对象与字节数组相互转换
- 将bitmap对象保存为一个图片文件