wxr
2022-11-24 2af932533ef851bf983385244e9912976dbd4daa
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
207
208
209
210
211
212
213
214
215
216
217
package com.lechange.demo.tools;
 
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
 
import com.lechange.common.log.Logger;
import com.lechange.opensdk.utils.LCOpenSDK_Utils;
import com.mm.android.deviceaddmodule.LCDeviceEngine;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class ImageHelper {
 
    private final static String TAG = "LCOpenSDK_Demo_ImageHelper";
    private static Options mDefaultOption;
    //最近最少使用算法的缓存策略
    private static LruCache<String, Drawable> mImageCache = new LruCache<String, Drawable>(100);
 
    static {
        mDefaultOption = new Options();
        //demo里面为了降低使用内存,图片缩小了一倍
        mDefaultOption.inSampleSize = 2;
        mDefaultOption.inPreferredConfig = Config.RGB_565;
    }
 
    public static void loadRealImage(final String url, final Handler handler) {
        downloadImage(url, "real", handler);
    }
 
    /**
     * 加载普通图片(缓存)
     *
     * @param url
     * @param handler
     */
    public static void loadCacheImage(final String url, final Handler handler) {
        String[] imageIDBuffer = url.split("[/?]");
        final String imageID = imageIDBuffer[imageIDBuffer.length - 2];
        Drawable drawable = mImageCache.get(imageID);
        if (drawable != null) {
            Message msg = new Message();
            msg.what = url.hashCode();
            msg.obj = drawable;
            handler.handleMessage(msg);
        } else {
            downloadImage(url, imageID, handler);
        }
    }
 
    /**
     * 加载加密图片(缓存)
     *
     * @param url
     * @param key
     * @param handler
     */
    public static void loadCacheImage(final String url, final String deviceId, String key, int position,final Handler handler) {
        String[] imageIDBuffer = url.split("[/?]");
        String imageID;
        if (imageIDBuffer.length - 2 > 0) {
            imageID = imageIDBuffer[imageIDBuffer.length - 2];
        } else {
            imageID = "";
        }
        Drawable drawable = mImageCache.get(imageID);
        if (drawable != null) {
            Message msg = new Message();
            msg.what = url.hashCode();
            msg.obj = drawable;
            msg.arg1=position;
            handler.handleMessage(msg);
        } else {
            downloadImage(url, imageID, deviceId, key,  position, handler);
        }
    }
 
    /**
     * 下载普通图片任务
     *
     * @param url
     * @param imageID
     * @param handler
     */
    private static void downloadImage(final String url, final String imageID, final Handler handler) {
        TaskPoolHelper.addTask(new TaskPoolHelper.RunnableTask(imageID) {
            @Override
            public void run() {
                Drawable drawable = null;
                try {
                    //创建一个url对象  
                    URL resurl = new URL(url);
                    //设置超时时间
                    HttpURLConnection urlConn = (HttpURLConnection) resurl.openConnection();
                    urlConn.setConnectTimeout(5000);
                    urlConn.setReadTimeout(5000);
                    //打开URL对应的资源输入流  
                    InputStream is = urlConn.getInputStream();
                    //从InputStream流中解析出图片 
                    Bitmap bitmap = BitmapFactory.decodeStream(is, null, mDefaultOption);
                    if (bitmap != null) {
                        drawable = new BitmapDrawable(bitmap);
                    }
                    //加入缓存
                    mImageCache.put(imageID, drawable);
                    //关闭输入流  
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // 解析设备列表信息
                handler.obtainMessage(url.hashCode(), drawable).sendToTarget(); // 发送成功消息到界面
            }
        });
    }
 
    /**
     * 下载加密图片任务
     *
     * @param url
     * @param imageID
     * @param key
     * @param handler
     */
    private static void downloadImage(final String url, final String imageID, final String deviceId, final String key, final int position, final Handler handler) {
        TaskPoolHelper.addTask(new TaskPoolHelper.RunnableTask(imageID) {
            @Override
            public void run() {
                Drawable drawable = null;
                try {
                    //创建一个url对象  
                    URL resurl = new URL(url);
                    //设置超时时间
                    HttpURLConnection urlConn = (HttpURLConnection) resurl.openConnection();
                    urlConn.setConnectTimeout(5000);
                    urlConn.setReadTimeout(5000);
                    int code = urlConn.getResponseCode();
                    Logger.e(TAG, "====getResponseCode, code=" + code + ", resurl.file=" + resurl.getFile());
                    //打开URL对应的资源输入流  
                    InputStream is = urlConn.getInputStream();
                    //从InputStream流中解析出图片  
                    ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
                    byte[] buff = new byte[500];
                    int rc = 0;
                    int length = 0;
                    while ((rc = is.read(buff, 0, 500)) > 0) {
                        length += rc;
                        swapStream.write(buff, 0, rc);
                    }
                    byte[] srcBuf = swapStream.toByteArray();
                    byte[] dstBuf = new byte[500000];
                    int[] dstLen = new int[1];
                    dstLen[0] = 500000;
                    Bitmap bitmap; //=null
                    //调用图片解密新接口:三码合一
                    int res = LCOpenSDK_Utils.decryptPic(LCDeviceEngine.newInstance().accessToken, srcBuf, length, deviceId, key, dstBuf, dstLen);
                    Logger.e(TAG, "====LCOpenSDK_Utils.decryptPic, res=" + res + ", length=" + length);
                    switch (res) {
                        case 0:  //解密成功
                            bitmap = BitmapFactory.decodeByteArray(dstBuf, 0, dstLen[0], mDefaultOption);
                            if (bitmap == null) {
                                String filepath = "sdcard/temp.jpg";
                                //String sd = Environment.getExternalStorageDirectory() + "/temp.jpg";
                                File file = new File(filepath);
                                OutputStream outputStream = new FileOutputStream(file);
                                outputStream.write(dstBuf);
                                outputStream.flush();
                                outputStream.close();
                                bitmap = BitmapFactory.decodeFile(filepath);
                                Logger.e(TAG, "BitmapFactory.decodeFile");
                            }
 
                            if (bitmap != null) {
                                drawable = new BitmapDrawable(bitmap);
                            }
                            break;
                        case 1://待解密数据不完整
                        case 3: //图片非加密
                            bitmap = BitmapFactory.decodeByteArray(srcBuf, 0, length, mDefaultOption);
                            if (bitmap != null) {
                                drawable = new BitmapDrawable(bitmap);
                            }
                            break;
                        default: //解密失败
                            break;
                    }
                    //加入缓存
                    mImageCache.put(imageID, drawable);
                    //关闭输入流
                    is.close();
                    swapStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // 解析设备列表信息
                handler.obtainMessage(url.hashCode(), position,0,drawable).sendToTarget(); // 发送成功消息到界面
            }
        });
    }
 
    public static void clear() {
        TaskPoolHelper.clearTask();
        mImageCache.evictAll();
    }
}