我正在使用jna来调用Java中的Magnification api函数。
MagImageScalingCallback.java
package jna.extra;
import com.sun.jna.Callback;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HRGN;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
public interface MagImageScalingCallback extends Callback{
public Boolean MagImageScalingCallback(HWND hwnd, Pointer srcdata,MAGIMAGEHEADER srcheader, Pointer destdata,MAGIMAGEHEADER destheader,RECT source,RECT clipped,HRGN dirty);
}
MAGIMAGEHEADER.java
package jna.extra;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.platform.win32.Guid.GUID;
public class MAGIMAGEHEADER extends com.sun.jna.Structure {
public int width;
public int height;
public GUID format;
public int stride;
public int offset;
public int cbsize;
public List getFieldOrder() {
return Arrays.asList("width","height","format","stride","offset","cbsize");
}
}
Magnification.java
package jna.extra;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Magnification extends StdCallLibrary {
Magnification INSTANCE = (Magnification) Native.loadLibrary("magnification", Magnification.class,
W32APIOptions.DEFAULT_OPTIONS);
public Boolean MagInitialize();
public boolean MagSetWindowFilterList(HWND hwndMag, DWORD dword, int i,
HWND[] excludeHWNDs);
public boolean MagSetWindowSource(HWND hwndMag, RECT sourceRect);
public void MagGetWindowFilterList(HWND hwndMag, DWORD dword, int i, HWND[] test);
public boolean MagSetImageScalingCallback(HWND hwndMag,MagImageScalingCallback MagImageScalingCallback);
public MagImageScalingCallback MagGetImageScalingCallback(HWND hwndMag);
}
WinGDIExtra.java
package jna.extra;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinGDI;
public interface WinGDIExtra extends WinGDI {
public DWORD SRCCOPY = new DWORD(0x00CC0020);
public DWORD SRCPAINT = new DWORD(0x00ee0086);
public DWORD SRCAND = new DWORD(0x008800c6);
public DWORD SRCINVERT = new DWORD(0x00660046);
public DWORD SRCERASE = new DWORD(0x00440328);
public DWORD NOTSRCCOPY = new DWORD(0x00330008);
public DWORD NOTSRCERASE = new DWORD(0x001100a6);
public DWORD MERGECOPY = new DWORD(0x00c000ca);
public DWORD MERGEPAINT = new DWORD(0x00bb0226);
public DWORD PATCOPY = new DWORD(0x00f00021);
public DWORD PATPAINT = new DWORD(0x00fb0a09);
public DWORD PATINVERT = new DWORD(0x005a0049);
public DWORD DSTINVERT = new DWORD(0x00550009);
public DWORD WHITENESS = new DWORD(0x00ff0062);
public DWORD BLACKNESS = new DWORD(0x00000042);
public DWORD CAPTUREBLT = new DWORD(0x00CC0020 | 0x40000000);
public DWORD Black = new DWORD(0x00000000);
public long WS_CHILD = 0x40000000L;
public long WS_VISIBLE = 0x10000000L;
public long MS_SHOWMAGNIFIEDCURSOR = 0x0001L;
public long WS_EX_TOPMOST = 0x00000008L;
public long WS_EX_LAYERED = 0x00080000;
public long WS_EX_TRANSPARENT = 0x00000020L;
public long WS_CLIPCHILDREN = 0x02000000L;
public long MW_FILTERMODE_EXCLUDE = 0;
}
我的代码
package jna.extra;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.HRGN;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import luz.winapi.jna.User32;
public class screenSkip {
public static void main(String[] args){
if(!Magnification.INSTANCE.MagInitialize()){
System.out.println("Cannot Intialize Magnification API");
System.exit(0);
}
RECT desktopRect= new RECT();
HWND desktop = User32.INSTANCE.GetDesktopWindow();
if(desktop==null){
System.out.println("Problem with Desktop");
System.exit(0);
}
if(!User32Extra.INSTANCE.GetWindowRect(desktop, desktopRect)){
System.err.println("Cannot get window rect");
System.exit(0);
}
HWND top = User32Extra.INSTANCE.CreateWindowEx(new DWORD(WinGDIExtra.WS_EX_TOPMOST | WinGDIExtra.WS_EX_LAYERED | WinGDIExtra.WS_EX_TRANSPARENT), "#32770", "Parent Window", new DWORD(WinGDIExtra.WS_CLIPCHILDREN), desktopRect.left, desktopRect.top, desktopRect.right-desktopRect.left, desktopRect.bottom-desktopRect.top, desktop, null, null, null);
if(top==null){
System.out.println("Problem while creating Parent Window and the error is "+Native.getLastError());
System.exit(0);
}
HWND hwndMag=null;
System.out.println(Native.getLastError());
hwndMag = User32Extra.INSTANCE.CreateWindowEx(null, "Magnifier", "MagWindow", new DWORD(WinGDIExtra.WS_CHILD | WinGDIExtra.MS_SHOWMAGNIFIEDCURSOR | WinGDIExtra.WS_VISIBLE), desktopRect.left, desktopRect.top, desktopRect.right-desktopRect.left, desktopRect.bottom-desktopRect.top, top, null, Kernel32.INSTANCE.GetModuleHandle(null), null);
if(hwndMag==null){
System.err.println("Problem while creating Magnifier Window and the error is "+Native.getLastError());
System.exit(0);
}
RECT sourceRect= new RECT();
if(!User32Extra.INSTANCE.GetWindowRect(desktop, sourceRect)){
System.err.println("Cannot get window rect");
System.exit(0);
}
final BufferedImage image = new BufferedImage(1366, 768, BufferedImage.TYPE_INT_RGB);
if(!Magnification.INSTANCE.MagSetImageScalingCallback(hwndMag,new MagImageScalingCallback() {
public Boolean MagImageScalingCallback(HWND hwnd, Pointer srcdata,
MAGIMAGEHEADER srcheader, Pointer destdata,
MAGIMAGEHEADER destheader, RECT source, RECT clipped, HRGN dirty) {
image.setRGB(0, 0, srcheader.width, srcheader.height, srcdata.getIntArray(0, srcheader.width * srcheader.height ), 0, srcheader.width);
return true;
}
})){
System.err.println("Error occured while setting callback");
System.exit(0);
}
if (!Magnification.INSTANCE.MagSetWindowSource(hwndMag, sourceRect))
{
System.err.println("Cannot copy");
System.exit(0);
}
try {
ImageIO.write(image, "JPEG", new File("printed1.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
if (!Magnification.INSTANCE.MagSetWindowSource(hwndMag, sourceRect))
{
System.err.println("Cannot copy");
System.exit(0);
}
}
}
如果调用MagSetWindowSource
函数,则调用MagImageScalingCallback
函数。
问题是如果我用jre7(64位)运行此代码一切正常。但是如果我在jre7(32位)中运行相同的代码,我会收到以下错误。
# # A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x10002a21, pid=5552, tid=4884 # # JRE version: Java(TM) SE Runtime Environment (7.0_80-b15) (build 1.7.0_80-b15) # Java VM: Java HotSpot(TM) Client VM (24.80-b11 mixed mode, sharing windows-x86 ) # Problematic frame: # C [jna6797525900717560222.dll+0x2a21] # # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows # # If you would like to submit a bug report, please visit: # http://bugreport.java.com/bugreport/crash.jsp # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug. # Stack: [0x05c60000,0x05cb0000], sp=0x05cae4d0, free space=313k Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) C [jna6797525900717560222.dll+0x2a21] j com.sun.jna.Pointer._getInt(J)I+0 j com.sun.jna.Pointer.getInt(J)I+6 j com.sun.jna.Pointer.getValue(JLjava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object;+340 j com.sun.jna.Structure.readField(Lcom/sun/jna/Structure$StructField;)Ljava/lang/Object;+168 j com.sun.jna.Structure.read()V+82 j com.sun.jna.CallbackReference$DefaultCallbackProxy.convertArgument(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;+330 j com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback([Ljava/lang/Object;)Ljava/lang/Object;+95 j com.sun.jna.CallbackReference$DefaultCallbackProxy.callback([Ljava/lang/Object;)Ljava/lang/Object;+2 v ~StubRoutines::call_stub V [jvm.dll+0x1429aa] V [jvm.dll+0x20743e] V [jvm.dll+0x142a2d] V [jvm.dll+0xcb7b2] V [jvm.dll+0xcd5df] C [jna6797525900717560222.dll+0x9dd2] C [jna6797525900717560222.dll+0xa47f] C [jna6797525900717560222.dll+0xc864] C [jna6797525900717560222.dll+0xcdee] C 0x00b80012 C [magnification.dll+0x5434] C [magnification.dll+0x5a2c] C [magnification.dll+0x5f5d] C [magnification.dll+0x612a] C [magnification.dll+0x28ab] C [USER32.dll+0x162fa] C [USER32.dll+0x16d3a] C [USER32.dll+0x1965e] C [USER32.dll+0x196c5] C [magnification.dll+0x22e2] C [jna6797525900717560222.dll+0xcc77] C [jna6797525900717560222.dll+0xc78a] C [jna6797525900717560222.dll+0x4561] C [jna6797525900717560222.dll+0x4d2e] j com.sun.jna.Function.invokeInt(I[Ljava/lang/Object;)I+0 j com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+315 j com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214 j com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341 j com.sun.proxy.$Proxy5.MagSetWindowSource(Lcom/sun/jna/platform/win32/WinDef$HWND;Lcom/sun/jna/platform/win32/WinDef$RECT;)Z+20 j org.redfire.screen.ScreenShare$CaptureScreen.run()V+674 j java.lang.Thread.run()V+11 v ~StubRoutines::call_stub V [jvm.dll+0x1429aa] V [jvm.dll+0x20743e] V [jvm.dll+0x142b75] V [jvm.dll+0x142bd7] V [jvm.dll+0xed5cf] V [jvm.dll+0x163c4c] V [jvm.dll+0x1646a7] V [jvm.dll+0x1a92f9] C [msvcr100.dll+0x5c556] C [msvcr100.dll+0x5c600] C [kernel32.dll+0x133aa] C [ntdll.dll+0x39f72] C [ntdll.dll+0x39f45] Java frames: (J=compiled Java code, j=interpreted, Vv=VM code) j com.sun.jna.Pointer._getInt(J)I+0 j com.sun.jna.Pointer.getInt(J)I+6 j com.sun.jna.Pointer.getValue(JLjava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object;+340 j com.sun.jna.Structure.readField(Lcom/sun/jna/Structure$StructField;)Ljava/lang/Object;+168 j com.sun.jna.Structure.read()V+82 j com.sun.jna.CallbackReference$DefaultCallbackProxy.convertArgument(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;+330 j com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback([Ljava/lang/Object;)Ljava/lang/Object;+95 j com.sun.jna.CallbackReference$DefaultCallbackProxy.callback([Ljava/lang/Object;)Ljava/lang/Object;+2 v ~StubRoutines::call_stub j com.sun.jna.Function.invokeInt(I[Ljava/lang/Object;)I+0 j com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+315 j com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214 j com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341 j com.sun.proxy.$Proxy5.MagSetWindowSource(Lcom/sun/jna/platform/win32/WinDef$HWND;Lcom/sun/jna/platform/win32/WinDef$RECT;)Z+20 j org.redfire.screen.ScreenShare$CaptureScreen.run()V+674 j java.lang.Thread.run()V+11 v ~StubRoutines::call_stub
如何解决这个问题?如何在32位JRE上完成这项工作? 谢谢!
答案 0 :(得分:3)
首先,MagImageScalingCallback
被定义为WINAPI
函数,这意味着您必须将回调定义为StdCallCallback
的子项。
其次,返回类型是一个简单的布尔值(你不应该使用布尔值)。
第三,回调的参数是MAGIMAGEHEADER
,而不是MAGIMAGEHEADER *
,而RECT
不是RECT *
,这意味着它们需要通过值传递;所以回调声明需要像这样改变:
我添加了一个RectByValue
类,其中包含以下内容:
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.Structure;
public class RectByValue extends WinDef.RECT implements Structure.ByValue {}
我使用以下内容更改了MagImageScalingCallback
类:
import com.sun.jna.win32.StdCallLibrary;
public interface MagImageScalingCallback extends StdCallLibrary.StdCallCallback {
public boolean MagImageScalingCallback(HWND hwnd, Pointer srcdata,MAGIMAGEHEADER.ByValue srcheader, Pointer destdata,MAGIMAGEHEADER.ByValue destheader,RectByValue source,RectByValue clipped,HRGN dirty);
}
您需要向ByValue
添加MAGIMAGEHEADER
支持;并且还使用SIZE_T
作为结构的最后一个元素:
import java.util.Arrays;
import java.util.List;
import com.sun.jna.platform.win32.Guid.GUID;
import com.sun.jna.platform.win32.BaseTSD;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
public class MAGIMAGEHEADER extends Structure {
public int width;
public int height;
public GUID format;
public int stride;
public int offset;
public BaseTSD.SIZE_T cbsize;
public List getFieldOrder() {
return Arrays.asList("width","height","format", "stride","offset","cbsize");
}
public static class ByValue extends MAGIMAGEHEADER implements Structure.ByValue {
public ByValue() {}
public ByValue(MAGIMAGEHEADER magimageheader) {
super (magimageheader.getPointer());
width = magimageheader.width;
height = magimageheader.height;
format = magimageheader.format;
stride = magimageheader.stride;
offset = magimageheader.offset;
cbsize = magimageheader.cbsize;
}
public ByValue(Pointer memory) {
super(memory);
}
}
public MAGIMAGEHEADER(Pointer memory) {
super(memory);
read();
}
public MAGIMAGEHEADER() {
}
}
我更改了屏幕尺寸以匹配screenSkip
上的显示,因此它new BufferedImage(2160, 1440...
代替1366, 786
,并将回调代码创建者更改为:
if(!Magnification.INSTANCE.MagSetImageScalingCallback(hwndMag, new MagImageScalingCallback() {
public boolean MagImageScalingCallback(HWND hwnd, Pointer srcdata,
MAGIMAGEHEADER.ByValue srcheader, Pointer destdata,
MAGIMAGEHEADER.ByValue destheader, RectByValue source, RectByValue clipped, HRGN dirty) {
image.setRGB(0, 0, srcheader.width, srcheader.height, srcdata.getIntArray(0, srcheader.width * srcheader.height ), 0, srcheader.width);
return true;
}
})){
System.err.println("Error occured while setting callback");
System.exit(0);
}
以32位或64位模式运行它的最终产品是一个名为的文件 这几乎是我能想到的最小化。
......呵呵......它给我一张32bit的空白图像。这似乎不正确。