gpt4 book ai didi

java - 使用 JNI 包装现有应用程序

转载 作者:行者123 更新时间:2023-11-30 05:15:16 26 4
gpt4 key购买 nike

大多数详细介绍如何开始使用 JNI 的文档都描述了如何使用 X-Code 构建新的 JNI 应用程序。谁能给我链接到如何在现有应用程序中使用 JNI 与 Objective-C 交互的描述。

最佳答案

注意:我已经从头开始完全重写了这个答案,现在我确信它有效;-)。

使用Rococoa而不是 JNI。

这是我制作的一个简短示例,它显示拍照对话框(基于您对 Stephen C 的回答的评论)。

/***
* INCOMPLETE: Doesn't have imports or anything like that.
***/

public interface Quartz extends Library
{
public static Quartz instance = (Quartz)Native.loadLibrary("Quartz", Quartz.class);
}

public interface IKPictureTaker extends NSObject
{
public static final _Class CLASS = Rococoa.createClass("IKPictureTaker", _Class.class);

public interface _Class extends NSClass
{
/**
* Returns a shared {@code IKPictureTaker} instance, creating it if necessary.
* @return an {@code IKPictureTaker} object.
*/
IKPictureTaker pictureTaker();
}

NSInteger runModal();
}

public class IKPictureTakerTest extends JFrame
{
public static void main(String[] args) throws Exception
{
// You need a GUI before this will work.
new IKPictureTakerTest().setVisible(true);

NSAutoreleasePool pool = NSAutoreleasePool.new_();

// Initialize the Quartz framework.
Quartz.instance.toString();

// Display the dialog.
IKPictureTaker pictureTaker = IKPictureTaker.CLASS.pictureTaker();
NSInteger result = pictureTaker.runModal();

if (result.intValue() == 0) // NSCancelButton
{
System.out.println("User cancelled.");
}
else
{
assert result.intValue() == 1; // NSOKButton
System.out.println("User chose an image.");
}

System.out.println(pictureTaker.inputImage()); // null if the user cancelled

pool.release();
}
}

如果你迷路了,试试洛 cocoa mailing lists 。开发人员非常有帮助。

关于java - 使用 JNI 包装现有应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1670859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com