android 그런 일들의 Bitmap, InputStream, Drawable,byte[],Base64 사이의 전환 관계

15776 단어 모바일 개발
  1           //  Bitmap   InputStream(   quality、100     、10    90%)
  2           public InputStream Bitmap2InputStream(Bitmap bm, int quality) {
  3               ByteArrayOutputStream baos = new ByteArrayOutputStream();
  4               bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
  5               InputStream is = new ByteArrayInputStream(baos.toByteArray());
  6               return is;
  7           }
  8           
  9           //  Bitmap   InputStream
 10           public InputStream Bitmap2InputStream(Bitmap bm) {
 11               ByteArrayOutputStream baos = new ByteArrayOutputStream();
 12               bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
 13               InputStream is = new ByteArrayInputStream(baos.toByteArray());
 14               return is;
 15           }
 16           
 17           //  InputStream   Bitmap
 18           public Bitmap InputStream2Bitmap(InputStream is) {
 19               return BitmapFactory.decodeStream(is);
 20           }
 21         
 22           // Drawable   InputStream
 23           public InputStream Drawable2InputStream(Drawable d) {
 24               Bitmap bitmap = this.drawable2Bitmap(d);
 25               return this.Bitmap2InputStream(bitmap);
 26           }
 27         
 28           // InputStream   Drawable
 29           public Drawable InputStream2Drawable(InputStream is) {
 30               Bitmap bitmap = this.InputStream2Bitmap(is);
 31               return this.bitmap2Drawable(bitmap);
 32           }
 33         
 34           // Drawable   byte[]
 35           public byte[] Drawable2Bytes(Drawable d) {
 36               Bitmap bitmap = this.drawable2Bitmap(d);
 37               return this.Bitmap2Bytes(bitmap);
 38           }
 39         
 40           // byte[]   Drawable
 41           public Drawable Bytes2Drawable(byte[] b) {
 42               Bitmap bitmap = this.Bytes2Bitmap(b);
 43               return this.bitmap2Drawable(bitmap);
 44           }
 45         
 46           // Bitmap   byte[]
 47           public byte[] Bitmap2Bytes(Bitmap bm) {
 48               ByteArrayOutputStream baos = new ByteArrayOutputStream();
 49               bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
 50               return baos.toByteArray();
 51           }
 52         
 53           // byte[]   Bitmap
 54           public Bitmap Bytes2Bitmap(byte[] b) {
 55               if (b.length !=   0) {
 56                   return BitmapFactory.decodeByteArray(b,   0, b.length);
 57               }
 58               return null;
 59           }
 60           
 61           //  byte[]   InputStream
 62           public InputStream Byte2InputStream(byte[] b) {
 63               ByteArrayInputStream bais = new ByteArrayInputStream(b);
 64               return bais;
 65           }
 66         
 67           //  InputStream   byte[]
 68           public byte[] InputStream2Bytes(InputStream is) {
 69               String str = "";
 70               byte[] readByte = new byte[1024];
 71               int readCount = -  1;
 72               try {
 73                   while ((readCount = is.read(readByte,   0, 1024)) != -  1) {
 74                       str += new String(readByte).trim();
 75                   }
 76                   return str.getBytes();
 77               } catch (Exception e) {
 78                   e.printStackTrace();
 79               }
 80               return null;
 81           }
 82           
 83           // Drawable   Bitmap
 84           public Bitmap drawable2Bitmap(Drawable drawable) {
 85               Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
 86                       drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
 87               Canvas canvas = new Canvas(bitmap);
 88               drawable.setBounds(  0,   0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
 89               drawable.draw(canvas);
 90               return bitmap;
 91           }
 92         
 93           // Bitmap   Drawable
 94           public Drawable bitmap2Drawable(Bitmap bitmap) {
 95               BitmapDrawable bd = new BitmapDrawable(bitmap);
 96               Drawable d = (Drawable) bd;
 97               return d;
 98           }
 99           
100           // Bitmap   Base64
101           public  String getImgStr(Bitmap bit){
102              ByteArrayOutputStream bos=new ByteArrayOutputStream();
103              bit.compress(CompressFormat.JPEG,  100, bos);//  100     
104              byte[] bytes=bos.toByteArray();
105              return Base64.encodeToString(bytes, Base64.DEFAULT);
106           }   
107                
108           // Base64   bitmap
109           public  Bitmap getimg(String str){
110               byte[] bytes;
111               bytes=Base64.decode(str,   0);
112               return BitmapFactory.decodeByteArray(bytes,   0, bytes.length);
113           }   

최근 많은 전환 관계 를 겪 었 기 때문에 정리 게시물 을 공유 했 다... 
전재 대상:https://www.cnblogs.com/yrhua/p/3946196.html

좋은 웹페이지 즐겨찾기