Java Image Save writeImageToBlob(int column, PreparedStatement psmt, Image image)

Here you can find the source of writeImageToBlob(int column, PreparedStatement psmt, Image image)

Description

write Image To Blob

License

Open Source License

Declaration

public static void writeImageToBlob(int column, PreparedStatement psmt, Image image)
            throws SQLException, IOException 

Method Source Code


//package com.java2s;
/*/*www.  j av  a  2 s. com*/
 * Copyright (C) 2010 Francisco Jos? Morero Peyrona. All Rights Reserved.
 *
 * This file is part of Tapas project: http://code.google.com/p/tapas-tpv/
 *
 * GNU Classpath is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the free
 * Software Foundation; either version 3, or (at your option) any later version.
 *
 * Tapas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Tapas; see the file COPYING.  If not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Main {
    public static void writeImageToBlob(int column, PreparedStatement psmt, Image image)
            throws SQLException, IOException {
        ImageIcon icon = null;

        if (image != null) {
            icon = new ImageIcon(image);
        }

        writeToBlob(column, psmt, imageToBytes(icon));
    }

    public static void writeToBlob(int index, PreparedStatement ps, byte[] data) throws SQLException {
        ByteArrayInputStream bais = null;

        if (data != null) {
            bais = new ByteArrayInputStream(data);
        }

        ps.setBinaryStream(index, bais);
    }

    public static byte[] imageToBytes(ImageIcon icon) throws IOException {
        if (icon == null) {
            return null;
        }

        Image img = icon.getImage();

        if (img.getWidth(null) < 1 && img.getHeight(null) < 1) {
            return null;
        }

        BufferedImage image = getBufferedImageFromImage(img);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ImageIO.write(image, "png", baos);

        return baos.toByteArray();
    }

    private static BufferedImage getBufferedImageFromImage(Image img) {
        // Crea un objeto BufferedImage con el ancho y alto de la Image
        BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        Graphics g = bufferedImage.createGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();

        return bufferedImage;
    }
}

Related

  1. write(Image image, ImageOutputStream ios, ImageWriter writer)
  2. writeImageIconToFile(ImageIcon icon, String imageType, File targetFile)