달력

4

« 2024/4 »

  • 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
728x90
반응형

안드로이드 - 스크린샷 소스코드

원본 출처: http://www.androidadb.com/source/pdn-slatedroid-read-only/eclair/sdk/screenshot/src/com/android/screenshot/Screenshot.java.html

Souce Code of com.android.screenshot.Screenshot


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
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.screenshot;
import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.Log;
import com.android.ddmlib.RawImage;
import com.android.ddmlib.Log.ILogOutput;
import com.android.ddmlib.Log.LogLevel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* Connects to a device using ddmlib and dumps its event log as long as the device is connected.
*/
public class Screenshot {
public static void main(String[] args) {
boolean device = false;
boolean emulator = false;
String serial = null;
String filepath = null;
boolean landscape = false;
if (args.length == 0) {
printUsageAndQuit();
}
// parse command line parameters.
int index = 0;
do {
String argument = args[index++];
if ("-d".equals(argument)) {
if (emulator || serial != null) {
printAndExit("-d conflicts with -e and -s", false /* terminate */);
}
device = true;
} else if ("-e".equals(argument)) {
if (device || serial != null) {
printAndExit("-e conflicts with -d and -s", false /* terminate */);
}
emulator = true;
} else if ("-s".equals(argument)) {
// quick check on the next argument.
if (index == args.length) {
printAndExit("Missing serial number after -s", false /* terminate */);
}
if (device || emulator) {
printAndExit("-s conflicts with -d and -e", false /* terminate */);
}
serial = args[index++];
} else if ("-l".equals(argument)) {
landscape = true;
} else {
// get the filepath and break.
filepath = argument;
// should not be any other device.
if (index < args.length) {
printAndExit("Too many arguments!", false /* terminate */);
}
}
} while (index < args.length);
if (filepath == null) {
printUsageAndQuit();
}
Log.setLogOutput(new ILogOutput() {
public void printAndPromptLog(LogLevel logLevel, String tag, String message) {
System.err.println(logLevel.getStringValue() + ":" + tag + ":" + message);
}
public void printLog(LogLevel logLevel, String tag, String message) {
System.err.println(logLevel.getStringValue() + ":" + tag + ":" + message);
}
});
// init the lib
// [try to] ensure ADB is running
String adbLocation = System.getProperty("com.android.screenshot.bindir"); //$NON-NLS-1$
if (adbLocation != null && adbLocation.length() != 0) {
adbLocation += File.separator + "adb"; //$NON-NLS-1$
} else {
adbLocation = "adb"; //$NON-NLS-1$
}
AndroidDebugBridge.init(false /* debugger support */);
try {
AndroidDebugBridge bridge = AndroidDebugBridge.createBridge(
adbLocation, true /* forceNewBridge */);
// we can't just ask for the device list right away, as the internal thread getting
// them from ADB may not be done getting the first list.
// Since we don't really want getDevices() to be blocking, we wait here manually.
int count = 0;
while (bridge.hasInitialDeviceList() == false) {
try {
Thread.sleep(100);
count++;
} catch (InterruptedException e) {
// pass
}
// let's not wait > 10 sec.
if (count > 100) {
System.err.println("Timeout getting device list!");
return;
}
}
// now get the devices
IDevice[] devices = bridge.getDevices();
if (devices.length == 0) {
printAndExit("No devices found!", true /* terminate */);
}
IDevice target = null;
if (emulator || device) {
for (IDevice d : devices) {
// this test works because emulator and device can't both be true at the same
// time.
if (d.isEmulator() == emulator) {
// if we already found a valid target, we print an error and return.
if (target != null) {
if (emulator) {
printAndExit("Error: more than one emulator launched!",
true /* terminate */);
} else {
printAndExit("Error: more than one device connected!",true /* terminate */);
}
}
target = d;
}
}
} else if (serial != null) {
for (IDevice d : devices) {
if (serial.equals(d.getSerialNumber())) {
target = d;
break;
}
}
} else {
if (devices.length > 1) {
printAndExit("Error: more than one emulator or device available!",
true /* terminate */);
}
target = devices[0];
}
if (target != null) {
try {
System.out.println("Taking screenshot from: " + target.getSerialNumber());
getDeviceImage(target, filepath, landscape);
System.out.println("Success.");
} catch (IOException e) {
e.printStackTrace();
}
} else {
printAndExit("Could not find matching device/emulator.", true /* terminate */);
}
} finally {
AndroidDebugBridge.terminate();
}
}
/*
* Grab an image from an ADB-connected device.
*/
private static void getDeviceImage(IDevice device, String filepath, boolean landscape)
throws IOException {
RawImage rawImage;
try {
rawImage = device.getScreenshot();
}
catch (IOException ioe) {
printAndExit("Unable to get frame buffer: " + ioe.getMessage(), true /* terminate */);
return;
}
// device/adb not available?
if (rawImage == null)
return;
if (landscape) {
rawImage = rawImage.getRotated();
}
// convert raw data to an Image
BufferedImage image = new BufferedImage(rawImage.width, rawImage.height,
BufferedImage.TYPE_INT_ARGB);
int index = 0;
int IndexInc = rawImage.bpp >> 3;
for (int y = 0 ; y < rawImage.height ; y++) {
for (int x = 0 ; x < rawImage.width ; x++) {
int value = rawImage.getARGB(index);
index += IndexInc;
image.setRGB(x, y, value);
}
}
if (!ImageIO.write(image, "png", new File(filepath))) {
throw new IOException("Failed to find png writer");
}
}
private static void printUsageAndQuit() {
// 80 cols marker: 01234567890123456789012345678901234567890123456789012345678901234567890123456789
System.out.println("Usage: screenshot2 [-d | -e | -s SERIAL] [-l] OUT_FILE");
System.out.println("");
System.out.println(" -d Uses the first device found.");
System.out.println(" -e Uses the first emulator found.");
System.out.println(" -s Targets the device by serial number.");
System.out.println("");
System.out.println(" -l Rotate images for landscape mode.");
System.out.println("");
System.exit(1);
}
private static void printAndExit(String message, boolean terminate) {
System.out.println(message);
if (terminate) {
AndroidDebugBridge.terminate();
}
System.exit(1);
}
}

 

728x90
반응형
:
Posted by mapagilove