Android將圖像轉換成流存儲與將流轉換成圖像


1、將圖片轉換成二進制流

public byte[] getBitmapByte(Bitmap bitmap){  
    ByteArrayOutputStream out = new ByteArrayOutputStream();  
    //參數1轉換類型,參數2壓縮質量,參數3字節流資源
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);  
    try {  
        out.flush();  
        out.close();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
    return out.toByteArray();  
}  

2、將二進制流轉換成圖片(Bitmap)

public Bitmap getBitmapFromByte(byte[] temp){  
    if(temp != null){  
        Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);  
        return bitmap;  
    }else{  
        return null;  
    }  
}  

將二進制流轉換成圖片(Drawable)

public Drawable getBitmapFromByte(byte[] temp){  
    if(temp != null){  
        Drawable drawable = Drawable.createFromStream(bais, "image");
        return drawable ;  
    }else{  
        return null;  
    }  
}  

3、將Bitmap轉換成Drawable

public static Bitmap drawableToBitmap(Drawable drawable){    
  
            int width = drawable.getIntrinsicWidth();    
  
            int height = drawable.getIntrinsicHeight();    
  
            Bitmap bitmap = Bitmap.createBitmap(width, height,    
  
                    drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888    
  
                            : Bitmap.Config.RGB_565);    
  
            Canvas canvas = new Canvas(bitmap);    
  
            drawable.setBounds(0,0,width,height);    
  
            drawable.draw(canvas);    
  
            return bitmap;    
  
                
  
        }    

Drawable drawable = new FastBitmapDrawable(bitmap);  


BitmapDrawable tempDrawable = (BitmapDrawable) drawable; tempDrawable.getBitmap();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM