栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

开源Java库在服务器端生成网页缩略图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

开源Java库在服务器端生成网页缩略图

首先想到的是使用AWT捕获屏幕抓取(请参见下面的代码)。您可以查看捕获JEdi​​torPane,JDIC


WebBrowser控件或SWT
浏览器(通过AWT嵌入支持)。后两个嵌入了本机浏览器(IE,Firefox),因此引入了依赖性。JEdi​​torPane HTML支持从HTML
3.2停止。这些可能都无法在无头系统上工作。

import java.awt.Component;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JLabel;public class Capture {    private static final int WIDTH = 128;    private static final int HEIGHT = 128;    private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);    public void capture(Component component) {        component.setSize(image.getWidth(), image.getHeight());        Graphics2D g = image.createGraphics();        try { component.paint(g);        } finally { g.dispose();        }    }    private BufferedImage getScaledImage(int width, int height) {        BufferedImage buffer = new BufferedImage(width, height,     BufferedImage.TYPE_INT_RGB);        Graphics2D g = buffer.createGraphics();        try { g.drawImage(image, 0, 0, width, height, null);        } finally { g.dispose();        }        return buffer;    }    public void save(File png, int width, int height) throws IOException {        ImageIO.write(getScaledImage(width, height), "png", png);    }    public static void main(String[] args) throws IOException {        JLabel label = new JLabel();        label.setText("Hello, World!");        label.setOpaque(true);        Capture cap = new Capture();        cap.capture(label);        cap.save(new File("foo.png"), 64, 64);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414024.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号