Android parses the image content into a byte array, converts the byte array to a bitmap object called by imageview, scales the image, saves the byte array as a file, and converts bitmap to BYT.

Source: Internet
Author: User
  1. Import java. Io. bufferedoutputstream;
  2. 04. Import java. Io. bytearrayoutputstream;
  3. 05. Import java. Io. file;
  4. 06. Import java. Io. fileoutputstream;
  5. 07. Import java. Io. ioexception;
  6. 08. Import java. Io. inputstream;
  7. 09.
  8. 10. Import Android. Graphics. Bitmap;
  9. 11. Import Android. Graphics. bitmapfactory;
  10. 12. Import Android. Graphics. matrix;
  11. 13.
  12. 14. Public class imagedispose {
  13. 15.
  14. 16.
  15. 17.
  16. 18 ./**
     
  17. 19. * @ Param parses the image content into a byte array
  18. 20. * @ Param instream
  19. 21. * @ return byte []
  20. 22. * @ throws exception
  21. 23 .*/
  22. 24. Public static byte [] readstream (inputstream instream) throws exception {
  23. 25. byte [] buffer = new byte [1, 1024];
  24. 26. Int Len =-1;
  25. 27. bytearrayoutputstream outstream = new bytearrayoutputstream ();
  26. 28. While (LEN = instream. Read (buffer ))! =-1 ){
  27. 29. outstream. Write (buffer, 0, Len );
  28. 30 .}
  29. 31. byte [] DATA = outstream. tobytearray ();
  30. 32. outstream. Close ();
  31. 33. instream. Close ();
  32. 34. return data;
  33. 35.
  34. 36 .}
  35. 37 ./**
     
  36. 38. * @ Param converts a byte array to a bitmap object that can be called by imageview.
     
  37. 39. * @ Param bytes
  38. 40. * @ Param opts
  39. 41. * @ return bitmap
  40. 42 .*/
  41. 43. Public static bitmap getpicfrombytes (byte [] bytes,
  42. 44. bitmapfactory. Options opts ){
  43. 45. If (Bytes! = NULL)
  44. 46. If (OPTs! = NULL)
  45. 47. Return bitmapfactory. decodebytearray (bytes, 0, bytes. length,
  46. 48. opts );
  47. 49. Else
  48. 50. Return bitmapfactory. decodebytearray (bytes, 0, bytes. Length );
  49. 51. return NULL;
  50. 52 .}
  51. 53 ./**
     
  52. 54. * @ Param Image Scaling
  53. 55. * @ Param bitmap object
  54. 56. * @ Param w the width to be scaled
  55. 57. * @ Param h the height to be scaled
  56. 58. * @ return newbmp new bitmap object
  57. 59 .*/
  58. 60. Public static bitmap zoombitmap (Bitmap bitmap, int W, int h ){
  59. 61. Int width = bitmap. getwidth ();
  60. 62. Int Height = bitmap. getheight ();
  61. 63. Matrix matrix = new matrix ();
  62. 64. Float scalewidth = (float) W/width );
  63. 65. Float scaleheight = (float) h/height );
  64. 66. matrix. postscale (scalewidth, scaleheight );
  65. 67. Bitmap newbmp = bitmap. createbitmap (bitmap, 0, 0, width, height,
  66. 68. matrix, true );
  67. 69. Return newbmp;
  68. 70 .}
  69. 71.
  70. 72 ./**
     
  71. 73. * convert bitmap to byte
  72. 74. * @ author heh
  73. 75. * @ edittime 2010-07-19 11:45:56 AM
     
  74. 76 .*/
  75. 77. Public static byte [] bitmap2bytes (Bitmap BM ){
  76. 78. bytearrayoutputstream baos = new bytearrayoutputstream ();
  77. 79. bm. Compress (bitmap. compressformat. PNG, 100, baos );
  78. 80. Return baos. tobytearray ();
  79. 81 .}
  80. 82 ./**
     
  81. 83. * Save the byte array as a file
  82. 84. * @ author heh
  83. 85. * @ edittime 2010-07-19 11:45:56 AM
     
  84. 86 .*/
  85. 87. Public static file getfilefrombytes (byte [] B, string outputfile ){
  86. 88. bufferedoutputstream stream = NULL;
  87. 89. File file = NULL;
  88. 90. Try {
  89. 91. File = new file (outputfile );
  90. 92. fileoutputstream fstream = new fileoutputstream (File );
  91. 93. Stream = new bufferedoutputstream (fstream );
  92. 94. Stream. Write (B );
  93. 95.} catch (exception e ){
  94. 96. E. printstacktrace ();
  95. 97.} finally {
  96. 98. If (stream! = NULL ){
  97. 99. Try {
  98. 100. Stream. Close ();
  99. 101.} catch (ioexception E1 ){
  100. 102. e1.printstacktrace ();
  101. 103 .}
  102. 104 .}
  103. 105 .}
  104. 106. Return file;
  105. 107 .}
  106. 108.
  107. 109 .}
Related Article

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.