lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
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
package cn.sinata.xldutils.utils;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
 
/**
 * base64编码文件
 * @author Administrator
 *
 */
public class Base64Util {
    
    public static String encode(String filename) throws Exception {
        File file = new File(filename);
 
        InputStream in = null;
        byte[] data = null;
 
        try {
            in = new FileInputStream(file);
            data = new byte[in.available()];
 
            in.read(data);
            in.close();
        } catch (Exception e) {
            if (in != null) {
                in.close();
            }
 
            throw e;
        }
        return Base64.encodeToString(data, Base64.DEFAULT);
    }
 
    //encode图片文件
    @SuppressWarnings("deprecation")
    public static String encodeImageFile(String filename) {
        byte[] data = null;
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filename, 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;
            Bitmap tembitmap = null;
            tembitmap = BitmapFactory.decodeFile(filename,
                    options);
            int degree=readPictureDegree(filename);
            tembitmap=adjustPhotoRotation(tembitmap, degree);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            tembitmap.compress(Bitmap.CompressFormat.JPEG, 70, baos);
            data = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }catch (OutOfMemoryError e) {
            System.gc();
            e.printStackTrace();
        }
        return Base64.encodeToString(data, Base64.DEFAULT);
    }
    
    /**
     * 旋转图片
     * @param bm
     * @param orientationDegree 角度
     * @return
     */
    private static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree){
 
        Matrix matrix = new Matrix();;  
        matrix.postRotate(orientationDegree);  
        // 创建新的图片  
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0,  
                bm.getWidth(), bm.getHeight(), matrix, true);  
        return resizedBitmap;
    }
    
    /** 
  * 读取照片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 String encodeImageBitmap(Bitmap bmp) throws Exception {
        byte[] data = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 80, baos);
            data = baos.toByteArray();
        } catch (Exception e) {
            throw e;
        }catch (OutOfMemoryError e) {
            System.gc();
            throw e;
        }
        return Base64.encodeToString(data, Base64.DEFAULT);
    }
    
    public static File decode(String str, String filename) throws Exception {
        File file = new File(filename);
        OutputStream out = null;
        try {
            if (!file.exists()) {
                new File(file.getParent()).mkdirs();
 
                file.createNewFile();
            }
            byte[] data = Base64.decode(str, Base64.DEFAULT);
            // byte[] data = decoder.decodeBuffer(str);
            for (int i = 0; i < data.length; ++i) {
                if (data[i] < 0) {
                    data[i] += 256;
                }
            }
 
            out = new FileOutputStream(filename);
 
            out.write(data);
            out.flush();
            out.close();
        } catch (Exception e) {
            if (out != null) {
                out.close();
            }
 
            file = null;
 
            throw e;
        }
        return file;
    }
}