大家好
我用JAVA实现了一个截图功能
页面是一个嵌入到HTML页面的用FLASH画的路线,关于线路的底色就是绿色的,
页面每分钟刷新一次,
根据XML数据来控制路线的颜色,
如果从XML得到其中一段路线不通的数据的话,则画面上的所对应这条数据的线段则变为红色或者灰色,
如果都顺畅的话,则通通是绿色,
我现在是通过下面的JAVA代码对页面进行截取并另存为图片的,
可是不管我怎么截取,明明是存在红色或者灰色的情况,可是截取后的图片却全都是绿色顺畅的,
跟页面显示的结果不符合,
请高手帮忙指点一下
难道JAVA不能对嵌入FLASH的HTML页面进行截图操作么?
package travl.action;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
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;import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;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;");
}
    
/**
 * 构造函数
 * 
 * @param url
 * @param maxWidth
 * @param maxHeight
 */
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);
}
try {
    
    // 输出到文件流
FileOutputStream out = new FileOutputStream("d:/"+fileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.flush();
out.close();
                    } 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.101:8080/PicViewSave/travl/snjt_gsgl.html", 1024, 768),
BorderLayout.CENTER);
frame.setSize(800, 600);
// 仅初始化,但不显示
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
NativeInterface.runEventPump();
}}