- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我会从下到上开始解释,这样你就会真正理解我想做什么,并更好地理解我的代码。
我正在创建一个库,让您捕获一个区域,无论捕获的是 gif 动画还是图像。捕获完成后,该库将返回一个对象,该对象包含 ByteArrayInputStream
和 createImage
等实用方法。
阅读本文时,您可以在此处访问图书馆:https://github.com/BenBeri/WiseCapturer/
现在这是一个关于我的库如何工作的虚拟示例:
您的应用程序使用捕获器类创建引导实例,并开始捕获:
public static void main(String[] args) throws AWTException {
final Bootstrap b = new Bootstrap(new ScreenshotCapturer());
b.beginCapture(new ScreenCaptureCallback() {
@Override
public void captureEnded(CapturedImage img) {
b.beginCapture(new ScreenCaptureCallback() {
@Override
public void captureEnded(CapturedImage img) {
JFrame frame = new JFrame();
frame.add(new JLabel(new ImageIcon(img.getBufferedImage())));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
});
}
监听器将返回 CapturedImage
,您可以使用它来做任何您想做的事情。现在有了这个例子,这应该让你捕获两次,一次又一次在你完成后,一旦完成,它将在 JFrame 窗口中显示第二次捕获。
现在我不是在谈论这个 JFrame。
这个问题只出现在 ScreeenshotCapturer
上,它会在 GifCapturer
实例上正常工作。
完成第一次捕获后,第二次捕获 JFrame 透明窗口不会出现,我在窗口工具栏中没有看到它,也没有在任何地方看到它,但应用程序仍在运行。
但是,正如我所说,如果我使用 GifCapturer 实例,它确实有效。
现在让我们调试我的库是如何工作的:
引导构造函数:
/**
* Bootstrap consturctor, let it bootstrap!
* @param c
* Capturer instance
* @throws AWTException
*/
public Bootstrap(Capturer c) throws AWTException {
this.capturer = c;
}
现在 Capturer 类初始化,它是抽象类,所有捕获器都有相同的构造函数:
public Capturer() throws AWTException {
this.camera = new CaptureCamera(this);
}
这会创建一个新的捕捉相机,这是我遇到问题的地方。CaptureCamera 的目的是让整个 JFrame,透明大小与我的屏幕相同,并在其中包含负责绘制选择矩形的 JPanel。
它的构造函数:
public CaptureCamera(Capturer c) throws AWTException {
this.c = c;
this.toolkit = Toolkit.getDefaultToolkit();
this.screen = this.toolkit.getScreenSize();
this.robot = new Robot();
this.selector = new SelectionCamera();
super.setSize(this.screen);
super.setUndecorated(true);
super.setBackground(new Color(255, 255, 255, 1));
// Listeners for area selection
super.addMouseListener(new SelectionAdapter(this, this.selector));
super.addMouseMotionListener(new SelectionMotion(this, this.selector));
super.add(this.selector);
}
好了,现在让我们来看看捕获是如何开始的。
bootstrap 中的 beginCapture
方法:
/**
* Starts capturing the screen, sends back a callback event with the
* captured file.
*
* The system saves a temporary file to send the file.
* @param c
* Callback instance
*/
public void beginCapture(ScreenCaptureCallback c) {
this.capturer.setCallback(c);
this.capturer.beginSelection();
}
setCallback
对于这个问题并不是很重要,所以 beginSelection
方法:
所有捕获器都一样
@Override
public void beginSelection() {
super.init();
this.setHotkeys();
super.getCamera().startSelection();
}
startSelection 方法(抱歉,重复的名称具有相同的含义,稍后会更改):
/**
* Starts area selection event
* @param c Capturer instance
*/
public void startSelection() {
super.setVisible(true);
}
好的,这是应该让 JFrame 可见的地方,我之前尝试过打印并且它显示为 true,但是 JFrame 在第二次尝试时没有显示。
现在框架可见,用户可以选择一个区域。选择后,鼠标适配器将执行 startCapturing
方法。
startCapturing
:
@Override
public void startCapturing(final int x, final int y, final int width, final int height) {
this.border = new GifCaptureBorder(x, y, width, height);
this.process = new TimerCaptureProcess(this, x, y, width, height);
Timer timer = new Timer();
timer.schedule(this.process, 0, 600);
}
`在 ScreenshotCapturer 中开始捕获:
@Override
public void startCapturing(int x, int y, int width, int height) {
Robot robot = super.getCamera().getRobot();
BufferedImage image = robot.createScreenCapture(new Rectangle(x, y, width, height));
super.disableSelectionFrame();
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(image, "png", stream);
super.setCaptureResult(stream);
super.finish();
} catch (IOException e) {
e.printStackTrace();
}
}
现在在 GifCapturer 中这个过程更长了,因为它实际上启动了一个 Timer
每隔 60ms
对每一帧进行截图。
要完成捕获 gif,请单击“输入”,我使用了 JKeyMaster检测热键。点击“ENTER”后,该方法将在GifCapturer
public void createGif() {
super.disableSelectionFrame();
AnimatedGifEncoder gif = new AnimatedGifEncoder();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
gif.start(stream);
gif.setDelay(1000);
this.border.updateProgress(10);
for(int i = 0; i < this.frames.size(); i++) {
gif.addFrame(this.frames.get(i));
}
this.border.updateProgress(50);
gif.finish();
super.setCaptureResult(stream);
this.border.updateProgress(100);
super.finish();
this.border.setVisible(false);
this.border = null;
}
差不多就是这样,如果我将使用 GifCapturer
两次,一切正常,但如果我将使用 ScreenshotCapturer
两次,JFrame 将不会出现在第二次时间!
我不太确定为什么,这可能是 Swing 中的错误吗?可能是因为 GifCapturer
需要更长的时间才能使框架可见?
我做错了什么?
最佳答案
好的,据我了解,您遇到的问题是这段代码...
基本上,当您调用 beginCapture
时,WiseCapturer API 允许您在屏幕上“拖动”透明矩形...
public static void main(String[] args) throws AWTException {
final Bootstrap b = new Bootstrap(new ScreenshotCapturer());
b.beginCapture(new ScreenCaptureCallback() {
@Override
public void captureEnded(CapturedImage img) {
b.beginCapture(new ScreenCaptureCallback() {
@Override
public void captureEnded(CapturedImage img) {
JFrame frame = new JFrame();
frame.add(new JLabel(new ImageIcon(img.getBufferedImage())));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
});
}
您遇到的问题是当调用 captureEnded
时(对于外部捕获),内部捕获过程没有说明(并且您不能“拖动”透明选择矩形)。 ..
这似乎是因为每当线程/事件队列 WiseCapturer 正在使用并且 captureEnded
事件不允许完成时您正在阻塞...
如果我做类似...
try {
final Bootstrap b = new Bootstrap(new ScreenshotCapturer());
b.beginCapture(new ScreenCaptureCallback() {
@Override
public void captureEnded(CapturedImage img) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
b.beginCapture(new ScreenCaptureCallback() {
@Override
public void captureEnded(CapturedImage img) {
System.out.println("...");
JFrame frame = new JFrame();
frame.add(new JLabel(new ImageIcon(img.getBufferedImage())));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
});
t.start();
}
});
System.out.println("Hello");
} catch (AWTException exp) {
exp.printStackTrace();
}
在外部 captureEnded
调用中启动一个新的 Thread
,我可以让它工作...
另外,不知道这个 API 的线程安全规则,我还使用了 SwingUtilities.invokeLater
try {
final Bootstrap b = new Bootstrap(new ScreenshotCapturer());
b.beginCapture(new ScreenCaptureCallback() {
@Override
public void captureEnded(CapturedImage img) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
b.beginCapture(new ScreenCaptureCallback() {
@Override
public void captureEnded(CapturedImage img) {
System.out.println("...");
JFrame frame = new JFrame();
frame.add(new JLabel(new ImageIcon(img.getBufferedImage())));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
});
}
});
System.out.println("Hello");
} catch (AWTException exp) {
exp.printStackTrace();
}
成功了......我也有点奇怪你为什么要这样做,但我就是这样
关于Java JFrame 在不可见后使用 .setVisible(true) 后不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25067871/
我的Angular-Component位于一个flexbox(id =“log”)中。可以显示或隐藏flexbox。 我的组件内部有一个可滚动区域,用于显示日志消息。 (id =“message-li
我真的很困惑 有一个 phpinfo() 输出: MySQL 支持 启用 客户端 API 版本 5.5.40 MYSQL_MODULE_TYPE 外部 phpMyAdmin 显示: 服务器类型:Mar
我正在研究这个 fiddle : http://jsfiddle.net/cED6c/7/我想让按钮文本在单击时发生变化,我尝试使用以下代码: 但是,它不起作用。我应该如何实现这个?任何帮助都会很棒
我应该在“dogs_cats”中保存表“dogs”和“cats”各自的ID,当看到数据时显示狗和猫的名字。 我有这三个表: CREATE TABLE IF NOT EXISTS cats ( id
我有一个字符串返回到我的 View 之一,如下所示: $text = 'Lorem ipsum dolor ' 我正在尝试用 Blade 显示它: {{$text}} 但是,输出是原始字符串而不是渲染
我无法让我的链接(由图像表示,位于页面左侧)真正有效地显示一个 div(包含一个句子,位于中间)/单击链接时隐藏。 这是我的代码: Practice
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
最初我使用 Listview 来显示 oracle 结果,但是最近我不得不切换到 datagridview 来处理比 Listview 允许的更多的结果。然而,自从切换到数据网格后,我得到的结果越来越
我一直在尝试插入一个 Unicode 字符 ∇ 或 ▽,所以它显示在 Apache FOP 生成的 PDF 中。 这是我到目前为止所做的: 根据这个基本帮助 Apache XSL-FO Input,您
我正在使用 node v0.12.7 编写一个 nodeJS 应用程序。 我正在使用 pm2 v0.14.7 运行我的 nodejs 应用程序。 我的应用程序似乎有内存泄漏,因为它从我启动时的大约 1
好的,所以我有一些 jQuery 代码,如果从下拉菜单中选择了带有前缀 Blue 的项目,它会显示一个输入框。 代码: $(function() { $('#text1').hide();
当我试图检查 Chrome 中的 html 元素时,它显示的是 LESS 文件,而 Firefox 显示的是 CSS 文件。 (我正在使用 Bootstrap 框架) 如何在 Chrome 中查看 c
我是 Microsoft Bot Framework 的新手,我正在通过 youtube 视频 https://youtu.be/ynG6Muox81o 学习它并在 Ubuntu 上使用 python
我正在尝试转换从 mssql 生成的文件到 utf-8。当我打开他的输出 mssql在 Windows Server 2003 中使用 notepad++ 将文件识别为 UCS-2LE我使用 file
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我正在尝试执行单击以打开/关闭一个 div 的功能。 这是基本的,但是,点击只显示 div,当我点击“关闭”时,没有任何反应。 $(".inscricao-email").click(function
假设我有 2 张卡片,屏幕上一次显示一张。我有一个按钮可以用其他卡片替换当前卡片。现在假设卡 1 上有一些数据,卡 2 上有一些数据,我不想破坏它们每个上的数据,或者我不想再次重建它们中的任何一个。
我正在使用 Eloquent Javascript 学习 Javascript。 我在 Firefox 控制台上编写了以下代码,但它返回:“ReferenceError:show() 未定义”为什么?
我正在使用 Symfony2 开发一个 web 项目,我使用 Sonata Admin 作为管理面板,一切正常,但我想要做的是,在 Sonata Admin 的仪表板菜单上,我需要显示隐藏一些菜单取决
我试图显示一个div,具体取决于从下拉列表中选择的内容。例如,如果用户从列表中选择“现金”显示现金div或用户从列表中选择“检查”显示现金div 我整理了样本,但样本不完整,需要接线 http://j
我是一名优秀的程序员,十分优秀!