-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJNAScreenShot.java
More file actions
156 lines (141 loc) · 6.17 KB
/
JNAScreenShot.java
File metadata and controls
156 lines (141 loc) · 6.17 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
package com.company;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.DataBufferUShort;
import java.awt.image.DirectColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import com.sun.jna.Native;
import com.sun.jna.win32.W32APIOptions;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.platform.win32.WinGDI;
public class JNAScreenShot {
public static BufferedImage getScreenshot(Rectangle bounds) {
WinDef.HDC windowDC = GDI.GetDC(USER.GetDesktopWindow());
WinDef.HBITMAP outputBitmap =
GDI.CreateCompatibleBitmap(windowDC,
bounds.width, bounds.height);
try {
WinDef.HDC blitDC = GDI.CreateCompatibleDC(windowDC);
try {
WinNT.HANDLE oldBitmap =
GDI.SelectObject(blitDC, outputBitmap);
try {
GDI.BitBlt(blitDC,
0, 0, bounds.width, bounds.height,
windowDC,
bounds.x, bounds.y,
GDI32.SRCCOPY);
} finally {
GDI.SelectObject(blitDC, oldBitmap);
}
WinGDI.BITMAPINFO bi = new WinGDI.BITMAPINFO(40);
bi.bmiHeader.biSize = 40;
boolean ok =
GDI.GetDIBits(blitDC, outputBitmap, 0, bounds.height,
(byte[]) null, bi, WinGDI.DIB_RGB_COLORS);
if (ok) {
WinGDI.BITMAPINFOHEADER bih = bi.bmiHeader;
bih.biHeight = -Math.abs(bih.biHeight);
bi.bmiHeader.biCompression = 0;
return bufferedImageFromBitmap(blitDC, outputBitmap, bi);
} else {
return null;
}
} finally {
GDI.DeleteObject(blitDC);
}
} finally {
GDI.DeleteObject(outputBitmap);
}
}
private static BufferedImage bufferedImageFromBitmap(WinDef.HDC blitDC,
WinDef.HBITMAP outputBitmap,
WinGDI.BITMAPINFO bi) {
WinGDI.BITMAPINFOHEADER bih = bi.bmiHeader;
int height = Math.abs(bih.biHeight);
final ColorModel cm;
final DataBuffer buffer;
final WritableRaster raster;
int strideBits =
(bih.biWidth * bih.biBitCount);
int strideBytesAligned =
(((strideBits - 1) | 0x1F) + 1) >> 3;
final int strideElementsAligned;
switch (bih.biBitCount) {
case 16:
strideElementsAligned = strideBytesAligned / 2;
cm = new DirectColorModel(16, 0x7C00, 0x3E0, 0x1F);
buffer =
new DataBufferUShort(strideElementsAligned * height);
raster =
Raster.createPackedRaster(buffer,
bih.biWidth, height,
strideElementsAligned,
((DirectColorModel) cm).getMasks(),
null);
break;
case 32:
strideElementsAligned = strideBytesAligned / 4;
cm = new DirectColorModel(32, 0xFF0000, 0xFF00, 0xFF);
buffer =
new DataBufferInt(strideElementsAligned * height);
raster =
Raster.createPackedRaster(buffer,
bih.biWidth, height,
strideElementsAligned,
((DirectColorModel) cm).getMasks(),
null);
break;
default:
throw new IllegalArgumentException("Unsupported bit count: " + bih.biBitCount);
}
final boolean ok;
switch (buffer.getDataType()) {
case DataBuffer.TYPE_INT: {
int[] pixels = ((DataBufferInt) buffer).getData();
ok = GDI.GetDIBits(blitDC, outputBitmap, 0, raster.getHeight(), pixels, bi, 0);
}
break;
case DataBuffer.TYPE_USHORT: {
short[] pixels = ((DataBufferUShort) buffer).getData();
ok = GDI.GetDIBits(blitDC, outputBitmap, 0, raster.getHeight(), pixels, bi, 0);
}
break;
default:
throw new AssertionError("Unexpected buffer element type: " + buffer.getDataType());
}
if (ok) {
return new BufferedImage(cm, raster, false, null);
} else {
return null;
}
}
private static final User32 USER = User32.INSTANCE;
private static final GDI32 GDI = GDI32.INSTANCE;
}
interface GDI32 extends com.sun.jna.platform.win32.GDI32,
com.sun.jna.platform.win32.WinGDI,
com.sun.jna.platform.win32.WinDef {
GDI32 INSTANCE =
(GDI32) Native.load(GDI32.class);
boolean BitBlt(HDC hdcDest, int nXDest, int nYDest,
int nWidth, int nHeight, HDC hdcSrc,
int nXSrc, int nYSrc, int dwRop);
HDC GetDC(HWND hWnd);
boolean GetDIBits(HDC dc, HBITMAP bmp, int startScan, int scanLines,
byte[] pixels, BITMAPINFO bi, int usage);
boolean GetDIBits(HDC dc, HBITMAP bmp, int startScan, int scanLines,
short[] pixels, BITMAPINFO bi, int usage);
boolean GetDIBits(HDC dc, HBITMAP bmp, int startScan, int scanLines,
int[] pixels, BITMAPINFO bi, int usage);
int SRCCOPY = 0xCC0020;
}
interface User32 extends com.sun.jna.platform.win32.User32 {
User32 INSTANCE = (User32) Native.load(User32.class, W32APIOptions.UNICODE_OPTIONS);
com.sun.jna.platform.win32.WinDef.HWND GetDesktopWindow();
}