-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathScreentShotUtil.java
executable file
·289 lines (261 loc) · 7.03 KB
/
ScreentShotUtil.java
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
package com.cj.ScreenShotUtil;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;
import android.widget.Toast;
public class ScreentShotUtil
{
private static final String TAG = "ScreentShotUtil";
private static final String CLASS1_NAME = "android.view.SurfaceControl";
private static final String CLASS2_NAME = "android.view.Surface";
private static final String METHOD_NAME = "screenshot";
private static ScreentShotUtil instance;
private Display mDisplay;
private DisplayMetrics mDisplayMetrics;
private Matrix mDisplayMatrix;
private WindowManager wm;
private SimpleDateFormat format;
private ScreentShotUtil()
{
}
public static ScreentShotUtil getInstance()
{
synchronized (ScreentShotUtil.class)
{
if (instance == null)
{
instance = new ScreentShotUtil();
}
}
return instance;
}
private Bitmap screenShot(int width, int height)
{
Log.i(TAG, "android.os.Build.VERSION.SDK : " + android.os.Build.VERSION.SDK_INT);
Class<?> surfaceClass = null;
Method method = null;
try
{
Log.i(TAG, "width : " + width);
Log.i(TAG, "height : " + height);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)
{
surfaceClass = Class.forName(CLASS1_NAME);
}
else
{
surfaceClass = Class.forName(CLASS2_NAME);
}
method = surfaceClass.getDeclaredMethod(METHOD_NAME, int.class, int.class);
method.setAccessible(true);
return (Bitmap) method.invoke(null, width, height);
}
catch (NoSuchMethodException e)
{
Log.e(TAG, e.toString());
}
catch (IllegalArgumentException e)
{
Log.e(TAG, e.toString());
}
catch (IllegalAccessException e)
{
Log.e(TAG, e.toString());
}
catch (InvocationTargetException e)
{
Log.e(TAG, e.toString());
}
catch (ClassNotFoundException e)
{
Log.e(TAG, e.toString());
}
return null;
}
/**
* Takes a screenshot of the current display and shows an animation.
*/
@SuppressLint("NewApi")
public void takeScreenshot(Context context, String fileFullPath)
{
if(fileFullPath == ""){
format = new SimpleDateFormat("yyyyMMddHHmmss");
String fileName = format.format(new Date(System.currentTimeMillis())) + ".png";
fileFullPath = "/data/local/tmp/" + fileName;
}
if(ShellUtils.checkRootPermission()){
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
ShellUtils.execCommand("/system/bin/screencap -p "+ fileFullPath,true);
}
}
else {
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2 && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mDisplay = wm.getDefaultDisplay();
mDisplayMatrix = new Matrix();
mDisplayMetrics = new DisplayMetrics();
// We need to orient the screenshot correctly (and the Surface api seems to take screenshots
// only in the natural orientation of the device :!)
mDisplay.getRealMetrics(mDisplayMetrics);
float[] dims =
{
mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels
};
float degrees = getDegreesForRotation(mDisplay.getRotation());
boolean requiresRotation = (degrees > 0);
if (requiresRotation)
{
// Get the dimensions of the device in its native orientation
mDisplayMatrix.reset();
mDisplayMatrix.preRotate(-degrees);
mDisplayMatrix.mapPoints(dims);
dims[0] = Math.abs(dims[0]);
dims[1] = Math.abs(dims[1]);
}
Bitmap mScreenBitmap = screenShot((int) dims[0], (int) dims[1]);
if (requiresRotation)
{
// Rotate the screenshot to the current orientation
Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(ss);
c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
c.rotate(degrees);
c.translate(-dims[0] / 2, -dims[1] / 2);
c.drawBitmap(mScreenBitmap, 0, 0, null);
c.setBitmap(null);
mScreenBitmap = ss;
if (ss != null && !ss.isRecycled())
{
ss.recycle();
}
}
// If we couldn't take the screenshot, notify the user
if (mScreenBitmap == null)
{
Toast.makeText(context, "screen shot fail", Toast.LENGTH_SHORT).show();
}
// Optimizations
mScreenBitmap.setHasAlpha(false);
mScreenBitmap.prepareToDraw();
saveBitmap2file(context, mScreenBitmap, fileFullPath);
}
}
}
//
public void saveBitmap2file(Context context, Bitmap bmp, String fileName)
{
int quality = 100;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, quality, baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
byte[] buffer = new byte[1024];
int len = 0;
File file = new File(fileName);
if (!file.exists())
{
try
{
file.getParentFile().mkdir();
file.getParentFile().createNewFile();
}
catch (IOException e)
{
Log.e(TAG, e.toString());
}
}
else
{
try
{
file.getParentFile().delete();
file.getParentFile().createNewFile();
}
catch (IOException e)
{
Log.e(TAG, e.toString());
}
}
FileOutputStream stream = null;
try
{
stream = new FileOutputStream(file);
while ((len = is.read(buffer)) != -1)
{
stream.write(buffer, 0, len);
}
stream.flush();
}
catch (FileNotFoundException e)
{
Log.i(TAG, e.toString());
}
catch (IOException e)
{
Log.i(TAG, e.toString());
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
Log.i(TAG, e.toString());
}
}
if (stream != null)
{
try
{
stream.close();
}
catch (IOException e)
{
Log.i(TAG, e.toString());
}
}
}
if (bmp != null && !bmp.isRecycled())
{
bmp.recycle();
}
}
/**
* @return the current display rotation in degrees
*/
private float getDegreesForRotation(int value)
{
switch (value)
{
case Surface.ROTATION_90:
return 360f - 90f;
case Surface.ROTATION_180:
return 360f - 180f;
case Surface.ROTATION_270:
return 360f - 270f;
}
return 0f;
}
}