整体思路是将数据库的图片,以blob格式读出,然后切割成等大小的小块再保存到数据库中。
使用mysql数据库和hibernate
代码:
public class WebCamMapSplittingAdapter
{
    public WebCamMapSplittingAdapter(Blob mapPic)
    {
        byte[] mapPicBytes = BlobHelp.writeFileToBinary(mapPic);
        sourceImage = Toolkit.getDefaultToolkit().createImage(mapPicBytes);
        // 问题:因为无法得到ImageObserver对象,所以必须要写两次,getHeight方法才能得到真实的高度??
        sourceImgHeight = sourceImage.getHeight(null);
        sourceImgHeight = sourceImage.getHeight(null);
        sourceImgWidth = sourceImage.getWidth(null);    }    Image sourceImage     = null; // 定义源图像对象
    int   sourceImgWidth  = 0;
    int   sourceImgHeight = 0;    public List splitMap ()
    {
        Image croppedImage = null; // 定义生成图片对象
        ImageFilter cropFilter = null; // 定义过滤器
        // 定义图片输出流
        int xIndex = 1;
        int yIndex = 1;
        // 定义返回图像数组
        List lstImage = new ArrayList();
        // 把传入的Blob类型转换成byte[]        try
        {
            // 循环对获取图的像进行分割
            for (int i = 0; i <= (sourceImgHeight / Integer
                    .parseInt(SystemProperties.IMG_HEIGHT)); i++)
            {
                for (int j = 0; j <= (sourceImgWidth / Integer
                        .parseInt(SystemProperties.IMG_WIDTH)); j++, xIndex++, yIndex++)
                {
                    // 切割图像
                    cropFilter = new CropImageFilter(j
                            * Integer.parseInt(SystemProperties.IMG_WIDTH), i
                            * Integer.parseInt(SystemProperties.IMG_HEIGHT),
                            Integer.parseInt(SystemProperties.IMG_WIDTH),
                            Integer.parseInt(SystemProperties.IMG_HEIGHT));                    croppedImage = Toolkit.getDefaultToolkit().createImage(
                            new FilteredImageSource(sourceImage.getSource(),
                                    cropFilter));
                    // 将Image转换成BufferdImage
                    BufferedImage thumbImage = new BufferedImage(Integer
                            .parseInt(SystemProperties.IMG_WIDTH), Integer
                            .parseInt(SystemProperties.IMG_HEIGHT),
                            BufferedImage.TYPE_INT_RGB);                    Graphics2D graphics2D = thumbImage.createGraphics();
                    graphics2D.setRenderingHint(
                            RenderingHints.KEY_INTERPOLATION,
                            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                    graphics2D.drawImage(croppedImage, 0, 0, Integer
                            .parseInt(SystemProperties.IMG_WIDTH), Integer
                            .parseInt(SystemProperties.IMG_HEIGHT), null);                    // 将BufferedImage转换成Blob
                    Blob byteImage = Hibernate
                            .createBlob(toByteArray(
                                    Integer
                                            .parseInt(SystemProperties.IMG_WIDTH),
                                    Integer
                                            .parseInt(SystemProperties.IMG_HEIGHT),
                                    thumbImage));
                    // 输出查看切割后的图像
                    FileOutputStream fileOut = new FileOutputStream(new File(
                            "d:/dd" + i + j + ".jpg"));
                    fileOut
                            .write(toByteArray(
                                    Integer
                                            .parseInt(SystemProperties.IMG_WIDTH),
                                    Integer
                                            .parseInt(SystemProperties.IMG_HEIGHT),
                                    thumbImage));
                    fileOut.flush();
                    fileOut.close();
                    // 问题:输出的图像全是黑色的?而不是原来的图像的一部分
                    lstImage.add(byteImage);
                }
            }        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return lstImage;
    }    public static byte[] toByteArray ( int width, int height,
            BufferedImage imageBuff ) throws java.io.IOException
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(imageBuff);
        param.setQuality(1.0f, false);
        encoder.setJPEGEncodeParam(param);
        encoder.encode(imageBuff);
        out.flush();
        return out.toByteArray();
    }    public int getSplitMapWidth ()
    {
        return sourceImgWidth;
    }}