Wicket从文件系统创建图像 [英] Wicket create images from file system

查看:142
本文介绍了Wicket从文件系统创建图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个项目,用户在点击产品时会打开一个带有产品名称的模态窗口。我还想在面板内的模态窗口中包含产品图像。图像存储在我的服务器上的目录中。

I am making a project in which user when click on a product a modal window open with the product name. I also want to include the product image on my modal window inside a panel. Images are stored on my server in a directory.

我指的是这个链接

我的面板html代码看起来像这样的 http://wicketinaction.com/2011/07/wicket -1-5-mounting-resources /

My panel html code look like this http://wicketinaction.com/2011/07/wicket-1-5-mounting-resources/

ItemOrderPanel.html

ItemOrderPanel.html

        <div>
            <li><a wicket:id="link"></a></li>
        </div>

ItemOrderPanel.java

ItemOrderPanel.java

    final ResourceReference imageResourceReference = new ImageResourceReference();
    String imageName = itm.getProductImage();
    final PageParameters parameters = new PageParameters();
    parameters.set("name", imageName);
    CharSequence urlForImage = getRequestCycle().urlFor(imageResourceReference,parameters);
    ExternalLink link = new ExternalLink("link", urlForImage.toString());
    link.setBody(Model.of(imageName));
    add(link);

在WicketApplication.java中

In WicketApplication.java

mountResource("/orderPage/{name}",new ImageResourceReference());

我对WicketApplication.java中的这一行有疑问。

I have doubt about this line in WicketApplication.java.

我创建了这样的资源文件

I have created resource file like this

ImageResourceReference.java

ImageResourceReference.java

public class ImageResourceReference extends ResourceReference{

public ImageResourceReference(){
    super(ImageResourceReference.class,"imagesDemo");
}

@Override
public IResource getResource() {
    return new ImageResource();
}

private static class ImageResource extends DynamicImageResource{

    private static final long serialVersionUID = 1L;

    @Override
    protected byte[] getImageData(Attributes attributes) {
        PageParameters parameters = attributes.getParameters();
        StringValue name = parameters.get("name");

                    byte[] imageBytes = null;
        if(name.isEmpty() == false)
            imageBytes = getImageAsBytes(name.toString());

        return imageBytes;
    }

    private byte[] getImageAsBytes(String label){
        BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.setColor(Color.BLACK);
        g.setBackground(Color.WHITE);
        g.clearRect(0, 0, image.getWidth(), image.getHeight());
        //g.setFont(new Font("SansSerif", Font.PLAIN, 48));
        g.drawString(label, 50, 50);
        g.dispose();

        Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = writers.next();
        if (writer == null) {
            throw new RuntimeException("JPG not supported?!");
        }

        final ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] imageBytes = null;
        try {

            ImageOutputStream imageOut = ImageIO.createImageOutputStream(out);
            writer.setOutput(imageOut);
            writer.write(image);
            imageOut.close();
            imageBytes = out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageBytes;


    }

    @Override
    public boolean equals(Object that){
        return that instanceof ImageResource;
    }

}

}

但是当我调试代码时,我发现控件不会进入内部 ImageResource类,这会返回字节。

But when i debug the code I found out that control is not coming to internal ImageResource class which is returning bytes.

我想在面板上显示图像。我面板上显示的链接是我存储在本地系统数据库中的链接。

I want to display the image picture on my panel. And the link that is showing on my panel is the link what i stored in my database which is of the local system.

任何帮助和建议表示赞赏!提前致谢。

Any help and advice appreciated! Thanks in advance.

推荐答案

最后我在ItemOrderPanel.java中确定了这段代码

Finally i settled on this code in ItemOrderPanel.java

add(new NonCachingImage("img", new AbstractReadOnlyModel<DynamicImageResource>(){
          @Override public DynamicImageResource getObject() {
            DynamicImageResource dir = new DynamicImageResource() {
              @Override protected byte[] getImageData(Attributes attributes) {
                  StringValue name = parameters.get("name");
                  byte[] imageBytes = null;
                    if(name.isEmpty() == false)
                        imageBytes = getImageAsBytes(name.toString());

                    return imageBytes;
              }
            };
            dir.setFormat("image/png");
            return dir;
          }
        }));

private byte[] getImageAsBytes(String label){
    byte[] imageBytes = null;
    try {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            InputStream inStream  = new FileInputStream(new File(label));
            copy(inStream, outStream);
            inStream.close();
            outStream.close();
            return outStream.toByteArray();

        } catch (IOException e) {
            e.printStackTrace();
          } 
     return imageBytes;


}

private void copy(InputStream source, OutputStream destination)
        throws IOException
    {

        // Transfer bytes from source to destination
            byte[] buf = new byte[1024];
            int len;
            while ((len = source.read(buf)) > 0) {
                destination.write(buf, 0, len);
            }
            source.close();
            destination.close();

    }

这篇关于Wicket从文件系统创建图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆