lmw
2023-03-11 4df5bb59e5fe9f9d140e5610f7772dd8a05a28d4
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package cn.sinata.xldutils.utils;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.media.ExifInterface;
import android.os.Build;
import android.view.Display;
import android.view.WindowManager;
import cn.sinata.xldutils.xldUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
 
/**
 *
 */
public class BitmapUtils {
 
    public static File compressImageFile(String filePath) {
        FileOutputStream fos=null;
        Bitmap tempBitmap = null;
        String path= xldUtils.PICDIR+filePath.hashCode()+".jpg";
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath, options);
            float scale = 1;
            int min=Math.min(options.outWidth, options.outHeight);
            int max=Math.max(options.outWidth, options.outHeight);
            if(min>=800){
                scale = max/800f;
            }else if(max>=1200){
                scale = max/1200f;
            }
            int sampleSize=new BigDecimal(scale).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            options.inJustDecodeBounds = false;
            options.inSampleSize = sampleSize;
            options.inPurgeable = true;
            options.inInputShareable = true;
 
            tempBitmap = BitmapFactory.decodeFile(filePath,
                    options);
            int degree=readPictureDegree(filePath);
            tempBitmap=adjustPhotoRotation(tempBitmap, degree);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            tempBitmap.compress(Bitmap.CompressFormat.JPEG, 70, baos);
            File file=new File(xldUtils.PICDIR);
            if (!file.exists()) {
                file.mkdirs();
            }
            fos=new FileOutputStream(path);
            fos.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        }catch (OutOfMemoryError e) {
            java.lang.System.gc();
            e.printStackTrace();
        }finally{
            if (fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (tempBitmap!=null) {
                tempBitmap.recycle();
            }
        }
        return new File(path);
    }
    /**
     * 旋转图片
     * @param bm bitmap
     * @param orientationDegree 角度
     * @return bitmap
     */
    private static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree){
 
        Matrix matrix = new Matrix();
        matrix.postRotate(orientationDegree);
        return Bitmap.createBitmap(bm, 0, 0,bm.getWidth(), bm.getHeight(), matrix, true);
    }
    /**
     * 读取照片exif信息中的旋转角度
     *
     * @param path
     *            照片路径
     * @return 角度
     */
    private static int readPictureDegree(String path) {
        int degree = 0;
        try {
            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;
    }
 
    public static Bitmap decodeBitmapFromPath(Context context,String filePath){
        int[] maxSize = calculateMaxBitmapSize(context);
        int maxWidth = maxSize[0];
        int maxHeight = maxSize[1];
        return decodeBitmapFromPath(context,filePath,maxWidth,maxHeight);
    }
 
    @SuppressWarnings({"SuspiciousNameCombination", "deprecation"})
    private static int[] calculateMaxBitmapSize(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
 
        Point size = new Point();
        int width, height;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            display.getSize(size);
            width = size.x;
            height = size.y;
        } else {
            width = display.getWidth();
            height = display.getHeight();
        }
        return new int[]{width,height};
    }
 
    public static Bitmap decodeBitmapFromPath(Context context,String filePath,int maxWidth,int maxHeight) {
        InputStream is = null;
        Bitmap bitmap = null;
        try {
            File file = new File(filePath);
            int sampleSize=calculateBitmapSampleSize(context,file,maxHeight,maxWidth);
            is = new FileInputStream(file);
            BitmapFactory.Options option = new BitmapFactory.Options();
            option.inSampleSize = sampleSize;
            option.inPreferredConfig = Bitmap.Config.RGB_565;
            option.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeStream(is, null, option);
            int degree = readPictureDegree(filePath);
            bitmap = adjustPhotoRotation(bitmap,degree);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
//            java.lang.System.gc();
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if (is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bitmap;
    }
 
    private static int calculateBitmapSampleSize(Context context, File file,int maxHeight,int maxWidth) throws IOException {
        InputStream is = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        try {
            is = new FileInputStream(file);
            BitmapFactory.decodeStream(is, null, options); // Just get image size
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Throwable t) {
                    // Do nothing
                }
            }
        }
        int sampleSize = 1;
        while (options.outHeight / sampleSize > maxHeight || options.outWidth / sampleSize > maxWidth) {
            sampleSize = sampleSize << 1;
        }
        return sampleSize;
    }
}