简体   繁体   中英

Need help create image from sqlite database

I'm trying to create image from SQLite database in Java Swing application. But it shows me errors. I'm using JDK 1.6, Java 6, SQLite 3, sqlitejdbc-v056.jar for connection SQLite and Ubuntu 11.04. I'm get sqlite connector from here . Here is the my code:

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;
    }

I get following errors:

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)

Please help me?

Your problem is that ImageIO.read() fails. This is probably due to this code:

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

Never use the type String for binary data and never ever use getBytes() - it depends on your platform default encoding. This means: It will take the Unicode characters in the String and turn them into whatever your computer thinks as good. That will destroy your binary data.

The correct solution is to use a BLOB column. See this answer how to write with BLOBs into sqlite: cannot save image as blob to sqlite

The code for reading is similar.

If that isn't an option, then use the encoding ISO-8859-1 which is a 1:1 mapping if you don't have Unicode characters (anything with a code point > 255) in your String .

Don't forget to write some unit tests to make sure your code works.

Use this code to get the bytes:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM