如何从 Access 附件字段中提取图像,并将其放置在 JLabel 中

Posted

技术标签:

【中文标题】如何从 Access 附件字段中提取图像,并将其放置在 JLabel 中【英文标题】:How to pull an image from a Access attachment field, and place it in a JLabel 【发布时间】:2019-05-22 16:53:50 【问题描述】:

我对 Java 还很陌生,所以我的知识有限。我有这个任务,我必须从 Access 数据库中获取数据并填充一个充满字段的对话框。我对典型字段没有任何问题,但我试图使附件字段工作时遇到了死胡同。

我尝试过使用我在网上看到的 .getByte() 方法,但我还不太了解 Attachment uncanaccess 类方法。任何人都可以帮助我或指导我正确的方向吗?以下是我如何填写其他字段的一些代码供参考:

JTextField_cod_distrib.setText(result.getLong("Cod_distribuitor")+"");  
JCheckBox_in_stoc.setSelected(result.getBoolean("In_stoc"));
JTextField_pret.setText(result.getFloat("Pret")+"");     JTextField_denumire_produs.setText(result.getString("Denumire_produs")+"");
JTextField_cod_produs.setText(result.getInt("Cod_produs")+"");
JTextField_ambalaj.setText(result.getString("Ambalaj")+"");  

【问题讨论】:

根据UCanAccess website,您必须通过ResultSet.getObject(columnName) 检索您的附件。然后你有一个net.ucanaccess.complex.Attachment 元素。 JavaDoc for this class 显示字节的 getData 方法。但是您没有指定要对附件做什么。 我想把它放在一个 JLabel 中(就像我的例子一样)。基本上我有一个带有字段和按钮的 JDialogBox,我可以循环浏览数据库表中的每个项目,显示它的属性。我想提取每个项目的附件并将其放在该对话框内的 JLabel 中,就像我在上面对 TextField 所做的那样。如何将此 Attachment 对象转换为图片,并将其存储在此标签中? 这样的东西应该可以工作:jlabel.setIcon(new ImageIcon(((Attachment)result.getObject("attachment")).getData())); @Sascha - Access 允许Attachment 字段中的每行有多个附件,因此getObject 返回一个由net.ucanaccess.complex.Attachment 对象组成的数组 请看我的回答。 【参考方案1】:

如果你知道数组中总是有一个附件,你可以这样做

jlabel.setIcon(new ImageIcon(getScaled(ImageIO.read(new ByteArrayInputStream(((Attachment[])result.getObject("attachment"))[0].getData())),120,120)));

否则您将不得不为每个附件添加一个 JLabel:

JPanel attachmentPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
Attachment[] attachments=(Attachment[])result.getObject("attachment");
for(Attachment attachment:attachments) 
    Image original=ImageIO.read(new ByteArrayInputStream(attachment.getData()));
    attachmentPanel.add(new JLabel(new ImageIcon(getScaled(original,120,120))));

//add the attachmentPanel to your component

来自https://docs.oracle.com/javase/tutorial/uiswing/examples/components/IconDemoProject/src/components/IconDemoApp.java

/**
     * 
     * Resizes an image using a Graphics2D object backed by a BufferedImage.
     * @param srcImg - source image to scale
     * @param w - desired width
     * @param h - desired height
     * @return - the new resized image
     */
    private BufferedImage getScaledImage(BufferedImage srcImg, int w, int h)
        double sw=srcImg.getWidth();
        double sh=srcImg.getHeight();
        double fw=w/sw;
        double fh=h/sh;
        if(fw<fh) w=(int)(sw*fh);
        else if(fh<fw) h=(int)(sh*fw);
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    

【讨论】:

太棒了。奇迹般有效。唯一的问题是图像的大小不一样,所以我需要缩放它们。知道如何使用 Attachment 对象执行此操作吗? 查看Oracle Icon Tutorial Code 我将getScaledImage 方法复制到我的答案中。 似乎不起作用。我收到错误“不兼容的类型:ImageIcon 无法转换为 Image”。这是代码行: JLabel_poza.setIcon(new ImageIcon(getScaledImage(new ImageIcon(((Attachment[])result.getObject("Poza"))[0].getData()),120,120)));跨度> 糟糕,我的错。应该是图片:JLabel_poza.setIcon(new ImageIcon(getScaled(ImageIO.read(new ByteArrayInputStream(((Attachment[])result.getObject("Poza"))[0].getData())),120,120))). 像魅力一样工作。唯一的问题是并非我所有的图像都是方形的。如何使缩放后的高度适应宽度?

以上是关于如何从 Access 附件字段中提取图像,并将其放置在 JLabel 中的主要内容,如果未能解决你的问题,请参考以下文章

在运行 ASP.NET 的网站中,如何显示存储在 MS Access 2007 附件类型中的图像

从 .Net 访问 Microsoft Access 2003 和 2007 BLOB 字段

从 Excel 中提取数据到 Access 数据库

如何检查 Access 中的附件字段是不是为空?

如何使用从访问数据库中提取的图像名称在网页上插入图像?

如何在 Access 中克隆 SharePoint 列表附件字段