我有个截取网页图片功能的CLASS,
想对这个CLASS进行每隔10秒钟执行一次,
我用Timer timer = new Timer();这个不好用,
因为我想调用的那个截图CLASS里有个
// 退出操作 
System.exit(0);
直接就跳出去了,
可是要是把这个System.exit(0);注释掉的话,只有第一次截图是正确的,以后截图就是错误的图片。
请问如何能对这个CLASS进行定时循环调用呢?
代码如下:package travl.action;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;import chrriis.dj.nativeswing.swtimpl.NativeComponent;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;public class PicGetView extends JPanel {
/**
 * 
 */
private static final long serialVersionUID = 1L; // 行分隔符
final static public String LS = System.getProperty("line.separator", "\n"); // 文件分割符
final static public String FS = System.getProperty("file.separator", "\\"); //以javascript脚本获得网页全屏后大小
final static StringBuffer jsDimension;

static {
jsDimension = new StringBuffer();
jsDimension.append("var width = 0;").append(LS);
jsDimension.append("var height = 0;").append(LS);
jsDimension.append("if(document.documentElement) {").append(LS);
jsDimension.append(
"  width = Math.max(width, document.documentElement.scrollWidth);")
.append(LS);
jsDimension.append(
"  height = Math.max(height, document.documentElement.scrollHeight);")
.append(LS);
jsDimension.append("}").append(LS);
jsDimension.append("if(self.innerWidth) {").append(LS);
jsDimension.append("  width = Math.max(width, self.innerWidth);")
.append(LS);
jsDimension.append("  height = Math.max(height, self.innerHeight);")
.append(LS);
jsDimension.append("}").append(LS);
jsDimension.append("if(document.body.scrollWidth) {").append(LS);
jsDimension.append(
"  width = Math.max(width, document.body.scrollWidth);")
.append(LS);
jsDimension.append(
"  height = Math.max(height, document.body.scrollHeight);")
.append(LS);
jsDimension.append("}").append(LS);
jsDimension.append("return width + ':' + height;");
} public PicGetView(final String url, final int maxWidth, final int maxHeight) {
super(new BorderLayout());
JPanel webBrowserPanel = new JPanel(new BorderLayout());
final String fileName = "test.jpg";
final JWebBrowser webBrowser = new JWebBrowser(null);
webBrowser.setBarsVisible(false);
webBrowser.navigate(url);
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
add(webBrowserPanel, BorderLayout.CENTER); JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4)); webBrowser.addWebBrowserListener(new WebBrowserAdapter() { // 监听加载进度
public void loadingProgressChanged(WebBrowserEvent e) {
// 当加载完毕时
if (e.getWebBrowser().getLoadingProgress() == 100) {
String result = (String) webBrowser
.executeJavascriptWithResult(jsDimension.toString());
int index = result == null ? -1 : result.indexOf(":");
NativeComponent nativeComponent = webBrowser
.getNativeComponent();
Dimension originalSize = nativeComponent.getSize();
Dimension imageSize = new Dimension(Integer.parseInt(result
.substring(0, index)), Integer.parseInt(result
.substring(index + 1)));
imageSize.width = Math.max(originalSize.width,
imageSize.width + 50);
imageSize.height = Math.max(originalSize.height,
imageSize.height + 50);
nativeComponent.setSize(imageSize);
BufferedImage image = new BufferedImage(imageSize.width,
imageSize.height, BufferedImage.TYPE_INT_RGB);
nativeComponent.paintComponent(image);
nativeComponent.setSize(originalSize);
// 当网页超出目标大小时
if (imageSize.width > maxWidth
|| imageSize.height > maxHeight) {
//截图部分图形
image = image.getSubimage(0, 0, maxWidth, maxHeight);
/*此部分为使用缩略图
int width = image.getWidth(), height = image
.getHeight();
 AffineTransform tx = new AffineTransform();
tx.scale((double) maxWidth / width, (double) maxHeight
/ height);
AffineTransformOp op = new AffineTransformOp(tx,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
//缩小
image = op.filter(image, null);*/
}
try {
// 输出图像
ImageIO.write(image, "jpg", new File(fileName));
} catch (IOException ex) {
ex.printStackTrace();
}
// 退出操作
System.exit(0);
}
}
} );
add(panel, BorderLayout.SOUTH); } public static void main(String[] args) {
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser
JFrame frame = new JFrame("以DJ组件保存指定网页截图");
frame.getContentPane().add(
new PicGetView("http://192.168.1.106:8080/PicViewSave/travl/snjt_gsgl.html", 1024, 768),
BorderLayout.CENTER);
frame.setSize(800, 600);
// 仅初始化,但不显示
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
NativeInterface.runEventPump();
}}

解决方案 »

  1.   

    不exit之后截取图片出错可能是你的程序资源没有实时释放或是初始化时有问题,而exit之后再重新打开刚好避开了此问题。
    你从这两个方面看下你的代码有什么问题没有。
      

  2.   

    // 退出操作
    System.exit(0);
    这个应该怎么改呢?
    这个一执行,所有的程序都退出了!
    有什么方法可以只释放当前的程序资源呢?
    请指教
    在线等
      

  3.   

    了解的多少给点建议啊!
    我没用过,给点思路也行,
    我把System.exit(0)注释掉后
    就在NativeInterface.runEventPump();这里报错
    我在这后面加上NativeInterface.close();也是在上句那里报错
      

  4.   

    CSDN这么大个论坛,就没个明白人么?
    都雪藏哪里去啦?
    呼叫大侠帮忙啊!
      

  5.   

    用多线程吧,在run()里面实现截图,
      

  6.   

    不了解NativeInterface
    因为NativeInterface.open();和NativeInterface.runEventPump();是静态方法,猜想只有在程序的开始和结束调用这两个方法,不应该在循环里调用这两个方法
    所以试试看
    public static void main(String[] args) {
            NativeInterface.open();
            for (int cnt=0; cnt<10; cnt++) { //循环放在这里
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser
                    JFrame frame = new JFrame("以DJ组件保存指定网页截图");
                    frame.getContentPane().add(
                            new PicGetView("http://192.168.1.106:8080/PicViewSave/travl/snjt_gsgl.html", 1024, 768),
                            BorderLayout.CENTER);
                    frame.setSize(800, 600);
                    // 仅初始化,但不显示
                    frame.invalidate();
                    frame.pack();
                    frame.setVisible(false);
                }
            });
            } //循环结束
            NativeInterface.runEventPump();
        }至于输出到指定路径,可以修改这里
    final String fileName = "test.jpg";
    // 输出图像
    ImageIO.write(image, "jpg", new File(fileName));