如何从 Access 附件字段中提取图像并将其放置在 JLabel 中
How to pull an image from a Access attachment field, and place it in a JLabel
我对 Java 还很陌生,所以我的知识有限。我有这个任务,我必须从 Access 数据库中获取数据并填写一个充满字段的对话框。我对典型字段没有问题,但我在尝试使附件字段工作时遇到了死胡同。
我已经尝试使用我在网上看到的 .getByte() 方法,但我还不太了解附件 uncanaccess class 方法。任何人都可以帮助我或指导我朝着正确的方向前进吗?这里有一些代码供参考,我是如何填写其他字段的:
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")+"");
如果您知道数组中始终只有一个附件,您可以
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
/**
*
* 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;
}
我对 Java 还很陌生,所以我的知识有限。我有这个任务,我必须从 Access 数据库中获取数据并填写一个充满字段的对话框。我对典型字段没有问题,但我在尝试使附件字段工作时遇到了死胡同。
我已经尝试使用我在网上看到的 .getByte() 方法,但我还不太了解附件 uncanaccess class 方法。任何人都可以帮助我或指导我朝着正确的方向前进吗?这里有一些代码供参考,我是如何填写其他字段的:
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")+"");
如果您知道数组中始终只有一个附件,您可以
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
/**
*
* 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;
}