Conversion Tool class "Go" between Android bitmap and drawable and byte[] and InputStream

Source: Internet
Author: User

  1. Package Com.soai.imdemo;
  2. Import Java.io.ByteArrayInputStream;
  3. Import Java.io.ByteArrayOutputStream;
  4. Import Java.io.InputStream;
  5. Import Android.graphics.Bitmap;
  6. Import Android.graphics.BitmapFactory;
  7. Import Android.graphics.Canvas;
  8. Import Android.graphics.PixelFormat;
  9. Import android.graphics.drawable.BitmapDrawable;
  10. Import android.graphics.drawable.Drawable;
  11. /**
  12. * Conversion Tool class between Bitmap and Drawable and byte[] and InputStream
  13. * @author Administrator
  14. *
  15. */
  16. Public class Formattools {
  17. private static Formattools tools = new Formattools ();
  18. public static Formattools getinstance () {
  19. if (Tools = = null) {
  20. Tools = new Formattools ();
  21. return tools;
  22. }
  23. return tools;
  24. }
  25. //Convert byte[] to InputStream
  26. Public InputStream Byte2inputstream (byte[] b) {
  27. Bytearrayinputstream Bais = new Bytearrayinputstream (b);
  28. return Bais;
  29. }
  30. //Convert InputStream to byte[]
  31. public byte[] Inputstream2bytes (InputStream is) {
  32. String str = "";
  33. byte[] ReadByte = new byte[1024];
  34. int readcount =-1;
  35. try {
  36. While ((Readcount = Is.read (ReadByte, 0, 1024x768))! =-1) {
  37. str + = new String (readbyte). Trim ();
  38. }
  39. return str.getbytes ();
  40. } catch (Exception e) {
  41. E.printstacktrace ();
  42. }
  43. return null;
  44. }
  45. //Convert bitmap to InputStream
  46. Public InputStream Bitmap2inputstream (Bitmap BM) {
  47. Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
  48. Bm.compress (Bitmap.CompressFormat.JPEG, BAOs);
  49. InputStream is = new Bytearrayinputstream (Baos.tobytearray ());
  50. return is;
  51. }
  52. //Convert bitmap to InputStream
  53. Public InputStream Bitmap2inputstream (Bitmap BM, int quality) {
  54. Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
  55. Bm.compress (Bitmap.CompressFormat.PNG, quality, BAOs);
  56. InputStream is = new Bytearrayinputstream (Baos.tobytearray ());
  57. return is;
  58. }
  59. //Convert InputStream to bitmap
  60. Public Bitmap Inputstream2bitmap (InputStream is) {
  61. return Bitmapfactory.decodestream (IS);
  62. }
  63. //drawable converted to InputStream
  64. Public InputStream Drawable2inputstream (drawable d) {
  65. Bitmap Bitmap = This.drawable2bitmap (d);
  66. return this .  Bitmap2inputstream (bitmap);
  67. }
  68. //InputStream converted to drawable
  69. Public drawable inputstream2drawable (InputStream is) {
  70. Bitmap Bitmap = this .  Inputstream2bitmap (IS);
  71. return this.bitmap2drawable (bitmap);
  72. }
  73. //drawable convert to byte[]
  74. public byte[] Drawable2bytes (drawable d) {
  75. Bitmap Bitmap = This.drawable2bitmap (d);
  76. return this .  Bitmap2bytes (bitmap);
  77. }
  78. ///byte[] converted to drawable
  79. Public drawable bytes2drawable (byte[] b) {
  80. Bitmap Bitmap = this .  Bytes2bitmap (b);
  81. return this.bitmap2drawable (bitmap);
  82. }
  83. //Bitmap convert to byte[]
  84. public byte[] Bitmap2bytes (Bitmap BM) {
  85. Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
  86. Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
  87. return Baos.tobytearray ();
  88. }
  89. ///byte[] converted to bitmap
  90. Public Bitmap Bytes2bitmap (byte[] b) {
  91. if (b.length! = 0) {
  92. return Bitmapfactory.decodebytearray (b, 0, b.length);
  93. }
  94. return null;
  95. }
  96. //drawable converted to bitmap
  97. Public Bitmap Drawable2bitmap (drawable drawable) {
  98. Bitmap Bitmap = Bitmap
  99. . CreateBitmap (
  100. Drawable.getintrinsicwidth (),
  101. Drawable.getintrinsicheight (),
  102. Drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888
  103. : Bitmap.Config.RGB_565);
  104. Canvas canvas = new canvas (bitmap);
  105. Drawable.setbounds (0, 0, Drawable.getintrinsicwidth (),
  106. Drawable.getintrinsicheight ());
  107. Drawable.draw (canvas);
  108. return bitmap;
  109. }
  110. //Bitmap converted to drawable
  111. Public drawable bitmap2drawable (Bitmap Bitmap) {
  112. bitmapdrawable bd = new bitmapdrawable (bitmap);
  113. drawable d = (drawable) BD;
  114. return D;
  115. }
  116. }

Conversion Tool class "Go" between Android bitmap and drawable and byte[] and InputStream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.