需要帮助从sqlite数据库创建图像

时间:2011-11-02 08:23:39

标签: java swing sqlite

我正在尝试在Java Swing应用程序中从SQLite数据库创建映像。但它显示了我的错误。我正在使用JDK 1.6,Java 6,SQLite 3,sqlitejdbc-v056.jar连接SQLite和Ubuntu 11.04。我从here获得了sqlite连接器。这是我的代码:

pos.res = pos.statement.executeQuery("select * from customer where card_id = "+card_id+";");

            while(pos.res.next()){
                if(pos.defaultParams != null){
                    pos.defaultParams.remove("pricelist_id");
                }else{
                    pos.defaultParams = new HashMap<String, Object>();
                }
                pos.defaultParams.put("pricelist_id", pos.res.getString("pricelist"));
                pos.jTextPane6.setText(pos.res.getString("name").toString());
                pos.customderId = Integer.parseInt(pos.res.getString("customer_id"));
                pos.jTextPane7.setText(pos.res.getString("department").toString());
                byte[] byt = pos.res.getString("photo").toString().getBytes();
                InputStream in = new ByteArrayInputStream(byt);
                BufferedImage image = ImageIO.read(in);
                ImageIO.write(image, "jpg",new File("/home/foo/Desktop/jj.jpg"));
                Graphics2D g = (Graphics2D)pos.jPanel4.getGraphics(); 
                g.drawImage(resize(image, 111, 100), null, 0, 0);
            }


private static BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage resizedImage = new BufferedImage(width, height,
        BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();
        return resizedImage;
    }

我收到以下错误:

Nov 2, 2011 4:12:04 PM pointofsale.setCustomer set
SEVERE: null
java.lang.IllegalArgumentException: image == null!
1819 [AWT-EventQueue-0] DEBUG pointofsale.PointOfSaleView  - endting setCustomer.set func
    at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:1038)
    at javax.imageio.ImageIO.getWriter(ImageIO.java:1581)
    at javax.imageio.ImageIO.write(ImageIO.java:1510)
    at pointofsale.setCustomer.set(setCustomer.java:65)
    at pointofsale.PointOfSaleView.focusGained(PointOfSaleView.java:1032)
    at java.awt.AWTEventMulticaster.focusGained(AWTEventMulticaster.java:220)
    at java.awt.Component.processFocusEvent(Component.java:6092)
    at java.awt.Component.processEvent(Component.java:5959)
    at java.awt.Container.processEvent(Container.java:2105)
    at java.awt.Component.dispatchEventImpl(Component.java:4564)
    at java.awt.Container.dispatchEventImpl(Container.java:2163)
    at java.awt.Component.dispatchEvent(Component.java:4390)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1881)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:936)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:551)
    at java.awt.Component.dispatchEventImpl(Component.java:4434)
    at java.awt.Container.dispatchEventImpl(Container.java:2163)
    at java.awt.Component.dispatchEvent(Component.java:4390)
    at sun.awt.X11.XWindow$1.run(XWindow.java:378)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:647)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:617)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

请帮帮我?

2 个答案:

答案 0 :(得分:2)

您的问题是ImageIO.read()失败了。这可能是由于这段代码所致:

pos.res.getString("photo").toString().getBytes();

永远不要将类型String用于二进制数据,永远不要使用getBytes() - 这取决于您的平台默认编码。这意味着:它将获取String中的Unicode字符并将它们转换为您的计算机认为合适的任何内容。这会破坏你的二进制数据。

正确的解决方案是使用BLOB列。请参阅此答案如何使用BLOB写入sqlite:cannot save image as blob to sqlite

阅读代码类似。

如果这不是一个选项,那么如果您的{{1}中没有Unicode字符(代码点数> 255的任何字符),请使用编码ISO-8859-1这是1:1映射。 }}

不要忘记编写一些单元测试以确保代码正常工作。

答案 1 :(得分:1)

使用此代码获取字节:

Blob blob = pos.res.getBlob("photo");
bytes[] b = blob.getBytes(1, (int) blob.length());