forked from SolveBugs/NormalCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtils.java
More file actions
307 lines (268 loc) · 10.3 KB
/
Copy pathImageUtils.java
File metadata and controls
307 lines (268 loc) · 10.3 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
public class ImageUtils {
private final static String MEIZU = "meizu";
private final static int MEIZU_IMG_KB = 200;//魅族图片压缩限制最大这尺寸
private final static int DEFAULT_IMG_KB = 250;
private final static int compress_granularity = 4;
/**
* 压缩图片
*
* @param image
* @return
*/
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
// 循环判断如果压缩后图片是否大于200kb,大于继续压缩
int max_kb = DEFAULT_IMG_KB;
if (MEIZU.equalsIgnoreCase(android.os.Build.BRAND)) {
max_kb = MEIZU_IMG_KB;
}
while (baos.toByteArray().length / 1024 > max_kb) {
// 重置baos
baos.reset();
// 这里压缩options%,把压缩后的数据存放到baos中
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
// 每次都减少10
options -= compress_granularity;
if (options < compress_granularity)
break;
}
// 把压缩后的数据baos存放到ByteArrayInputStream中
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
// 把ByteArrayInputStream数据生成图片
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}
/**
* 截取scrollview的屏幕
*
* @param scrollView
* @return
*/
public static Bitmap getBitmapByView(ScrollView scrollView) {
int h = 0;
Bitmap bitmap = null;
// 获取scrollview实际高度
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
scrollView.getChildAt(i).setBackgroundColor(
Color.parseColor("#E6E9ED"));
}
if (h == 0 || scrollView.getWidth() == 0) {
return null;
}
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
canvas.drawARGB(0xFF, 0xFF, 0xFF, 0xFF);
scrollView.draw(canvas);
return compressImage(bitmap);
}
public static Bitmap getBitmapByListView(ListView listView) {
int h = 0;
Bitmap bitmap = null;
// 获取listView实际高度
for (int i = 0; i < listView.getChildCount(); i++) {
h += listView.getChildAt(i).getHeight();
// listView.getChildAt(i).setBackgroundColor(
// Color.parseColor("#ffffff"));
}
if (h == 0 || listView.getWidth() == 0) {
return null;
}
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(listView.getWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
listView.draw(canvas);
return compressImage(bitmap);
}
public static Bitmap getBitmapByPullToRefreshListView(PullToRefreshListView listView) {
int h = 0;
Bitmap bitmap = null;
// 获取listView实际高度
for (int i = 0; i < listView.getChildCount(); i++) {
h += listView.getChildAt(i).getHeight();
// listView.getChildAt(i).setBackgroundColor(
// Color.parseColor("#ffffff"));
}
if (h == 0 || listView.getWidth() == 0) {
return null;
}
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(listView.getWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
listView.draw(canvas);
return compressImage(bitmap);
}
public static Bitmap getScrollBitmap(Activity activity, ScrollView scrollView) {
int h = 0;
Bitmap bitmap = null;
// 获取listView实际高度
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
}
// 创建对应大小的bitmap
if (h == 0 || scrollView.getWidth() == 0) {
return null;
}
h = Math.min(h, Utils.getScreenHeight(activity)); //防止OOM
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
scrollView.draw(canvas);
return bitmap;
}
public static Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent
// for the duration of this operation
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
public static Bitmap takeScreenShot(Activity activity) {
// View是你需要截图的View
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
// 获取状态栏高度
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
// 获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
// 去掉标题栏
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, b1.getHeight()
- statusBarHeight);
view.destroyDrawingCache();
return b;
}
public static String saveBitmapPNG(Context context, Bitmap imageBitmap) {
if (imageBitmap == null) {
return null;
}
String fileName = String.valueOf(System.currentTimeMillis()) + StringPool.JPG;
File savefile = new File(Utils.getImageFileDir(context), fileName);
if (savefile.exists()) {
boolean delete = savefile.delete();
Utils.printFileDirState(delete);
}
FileOutputStream out = null;
try {
out = new FileOutputStream(savefile, false);
if (null != out) {
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
}
} catch (FileNotFoundException e) {
com.dnurse.common.logger.Log.printThrowable(e);
return null;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return savefile.getPath();
}
public static String uploadPic(String path, AppContext appContext) {
User user = appContext.getActiveUser();
if (user != null && !TextUtils.isEmpty(path)) {
File file = new File(path);
if (!file.exists()) {
return null;
}
String sn = user.getSn();
HashMap<String, String> h1 = new HashMap<String, String>();
h1.put("sn", sn);
HashMap<String, File> h2 = new HashMap<String, File>();
h2.put("file", new File(path));
try {
String reply = BaseClient.post(appContext, DataURLs.UPLOAD_PIC, Utils.rebuildMap(h1, user), h2);
JSONObject replyJson = new JSONObject(reply);
if (NetRetKeys.SUCCESS_CODE == replyJson.optInt(NetRetKeys.STATE_CODE)) {
path = replyJson.optString(NetRetKeys.DATA);
return path;
}
/* 图片上传失败,先返回本地路径 */
return path;
} catch (Exception e) {
/* 图片上传失败,先返回本地路径 */
return path;
}
}
return "";
}
public static void copyAssetToSD(Context context, String pic) throws IOException {
OutputStream myOutput = new FileOutputStream(Utils.getImageFileDir(context) + pic);
try {
InputStream myInput = context.getAssets().open(pic);
byte[] buffer = new byte[1024];
int length = myInput.read(buffer);
while (length > 0) {
myOutput.write(buffer, 0, length);
length = myInput.read(buffer);
}
myOutput.flush();
} finally {
if (myOutput != null) {
myOutput.close();
}
}
}
private static void streamToSD(InputStream myInput, String path) throws IOException {
OutputStream myOutput = null;
try {
myOutput = new FileOutputStream(path);
byte[] buffer = new byte[1024];
int length = myInput.read(buffer);
while (length > 0) {
myOutput.write(buffer, 0, length);
length = myInput.read(buffer);
}
myOutput.flush();
myInput.close();
} finally {
if (myOutput != null) {
myOutput.close();
}
}
}
public static String assetSaveToSD(Context context, String resName) {
String baseUrl = Utils.getImageFileDir(context).getAbsolutePath();
String path = baseUrl + "/" + resName;
if (!new File(path).exists()) {
try {
streamToSD(context.getAssets().open(resName), path);
} catch (IOException e) {
}
}
return path;
}
}