Auf Thema antworten

Habe ma sowas für Hibernate gemacht. Das müsste das Gesuchte drin sein


[code=Java]public class ImageUserType implements UserType

{

private static Logger logger=Logger.getLogger(ImageUserType.class);


public int[] sqlTypes()

    {

    return new int[]

                {

                Types.BLOB

                };

    }


public Class returnedClass()

    {

    return Image.class;

    }


public boolean isMutable()

    {

    return false;

    }


public Object deepCopy(Object o) throws HibernateException

    {

    return o;

    }


public Serializable disassemble(Object o) throws HibernateException

    {

    return (Serializable)o;

    }


public Object assemble(Serializable o,Object arg1) throws HibernateException

    {

    return o;

    }


public Object replace(Object o,Object arg1,Object arg2) throws HibernateException

    {

    return o;

    }


public boolean equals(Object arg0,Object arg1) throws HibernateException

    {

    if(arg0==arg1)

        {

        return true;

        }

    if(arg0==null||arg1==null)

        {

        return false;

        }

    return arg0.equals(arg1);

    }


public int hashCode(Object o) throws HibernateException

    {

    return o.hashCode();

    }


public Object nullSafeGet(ResultSet rs,String[] names,Object arg2) throws HibernateException,SQLException

    {


    Blob blob=rs.getBlob(names[0]);

    if(blob==null)

        {

        return null;


        }

    else

        {

        return convertByteToImage(blob.getBytes((long)1,(int)blob.length()));

        }

    }


public void nullSafeSet(PreparedStatement st,Object image,int index) throws HibernateException,SQLException

    {

    st.setBytes(index,convertImageToBlob((Image)image));

    }


private byte[] convertImageToBlob(Image image)

    {

    if(image==null)

        {

        return null;

        }

    ByteArrayOutputStream baos=new ByteArrayOutputStream();

    try

        {

        int width=image.getWidth(null);

        int height=image.getHeight(null);

        BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        bi.getGraphics().drawImage(image,0,0,null);

        ImageIO.write(bi,"JPEG",baos);

        }

    catch(IOException ex)

        {

        logger.error("Fehler beim erzeugen des Bytearrays: "+ex.getMessage());

        }

    return baos.toByteArray();

    }


private Image convertByteToImage(byte[] bytearray)

    {

    Image image=Toolkit.getDefaultToolkit().createImage(bytearray);

    JFrame frm=new JFrame();

    MediaTracker mt=new MediaTracker(frm);

    mt.addImage(image,0);

    try

        {

        mt.waitForAll();

        }

    catch(InterruptedException ex)

        {

        logger.error("Fehler beim erzeugen des Image: "+ex.getMessage());

        }

    return image;

    }


}

[/code]



Oben