android_Bitmap

Bitmap實現在android.graphics包中。但是Bitmap類的構造函數是私有的

BitmapFactory可以從一個指定文件中,利用decodeFile()解出Bitmap

也可以定義的圖片資源中,利用decodeResource()解出Bitmap

 

Options的下列屬性,可以指定decode的選項:

·        inPreferredConfig 指定decode到內存中,手機中所採用的編碼,可選值定義在Bitmap.Config中。缺省值是ARGB_8888

·        inJustDecodeBounds 如果設置爲true,並不會把圖像的數據完全解碼,亦即decodeXyz()返回值爲null,但是OptionsoutAbc中解出了圖像的基本信息。

·        inSampleSize 設置decode時的縮放比例。

利用Options的這些值就可以高效的得到一幅縮略圖。

先設置inJustDecodeBounds= true,調用decodeFile()得到圖像的基本信息[Step#2~4]

利用圖像的寬度(或者高度,或綜合)以及目標的寬度,得到inSampleSize值,再設置inJustDecodeBounds= false,調用decodeFile()得到完整的圖像數據[Step#5~8]

先獲取比例,再讀入數據,如果欲讀入大比例縮小的圖,將顯著的節約內容資源。有時候還會讀入大量的縮略圖,這效果就更明顯了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
     * 獲取圖片在數據庫的路徑
     * @param uri
     * @return
     */
    private String getImgPath(Uri uri){
        String img_path = "";
        if(uri != null){
            String[] proj = { MediaStore.Images.Media.DATA };
            Cursor actualimagecursor = managedQuery(uri,proj,null,null,null);
            int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            actualimagecursor.moveToFirst();
            img_path = actualimagecursor.getString(actual_image_column_index);
        }
        return img_path;
}
  
/**
     * 獲取bitmap的Uri
     * @param photo
     * @return
     */
    private Uri getBitMapUri(Bitmap photo){
        File file = bitmapToFile(photo);
        return Uri.fromFile(file);
    }
  
private File bitmapToFile(Bitmap photo){
        File fileDir = new File(Constant.USER_ICON_DIR);
        if (!fileDir.exists()) {
            fileDir.mkdir();
        }else{
            if(fileDir.isDirectory()) {
                File[] childFiles = fileDir.listFiles();
                for (File file : childFiles) {
                    file.delete();
                }
            }
        }
        File f = new File(fileDir, System.currentTimeMillis() + ".jpeg");
        FileOutputStream fOut = null;
        try {
            if (f.exists()) {
                f.delete();
            }
            f.createNewFile();
            fOut = new FileOutputStream(f);
            photo.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
            fOut.flush();
        catch (Exception e) {
        finally {
            try {
                fOut.close();
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        return f;
    }
  
/**
     * 獲取圖片的存儲角度
     * @param path
     * @return
     */
    private int getBitmapDegree(String path) {
        int degree = 0;
        try {
            // 從指定路徑下讀取圖片,並獲取其EXIF信息
            ExifInterface exifInterface = new ExifInterface(path);
            // 獲取圖片的旋轉信息
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }
  
/**
     * 旋轉bitmap
     * @param bm
     * @param degree
     * @return
     */
    public Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
        Bitmap returnBm = null;
        // 根據旋轉角度,生成旋轉矩陣
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        try {
            // 將原始圖片按照旋轉矩陣進行旋轉,並得到新的圖片
            returnBm = Bitmap.createBitmap(bm, 00, bm.getWidth(), bm.getHeight(), matrix, true);
        catch (OutOfMemoryError e) {
        }
        if (returnBm == null) {
            returnBm = bm;
        }
        if (bm != returnBm) {
            bm.recycle();
        }
        return returnBm;
    }
  
  
/** 
     * *drawable 轉化爲bitmap 
     *  
     * @param drawable 
     * @return bitmap 
     */  
    public static Bitmap drawableToBitmap(Drawable drawable) {  
        if (drawable instanceof BitmapDrawable) {  
            return ((BitmapDrawable) drawable).getBitmap();  
        else if (drawable instanceof NinePatchDrawable) {  
            // 取 drawable 的長寬  
            int w = drawable.getIntrinsicWidth();  
            int h = drawable.getIntrinsicHeight();  
   
            // 取 drawable 的顏色格式  
            Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
                    : Bitmap.Config.RGB_565;  
            // 建立對應 bitmap  
            Bitmap bitmap = Bitmap.createBitmap(w, h, config);  
   
            // 建立對應 bitmap 的畫布  
            Canvas canvas = new Canvas(bitmap);  
            drawable.setBounds(00, w, h);  
            // 把 drawable 內容畫到畫布中  
            drawable.draw(canvas);  
   
            return bitmap;  
        else {  
            return null;  
        }  
    }  
   
    /** 
     *  
     * @param bm 
     * @return Byte 
     */  
    public byte[] Bitmap2Bytes(Bitmap bm) {  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
        return baos.toByteArray();  
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章