反序列化图像对象

时间:2014-09-19 04:27:35

标签: java deserialization

我想反序列化图像的对象。 但我无法理解我得到的错误。 我已将图像存储为.ser文件中的对象。 但是当我读到该文件时,我收到了一个错误。 请有人帮助我......

这是我的节目......

package deserializeDemo;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;



import javax.imageio.ImageIO;

public class DeserializeDemo {
    @SuppressWarnings("rawtypes")
    class ImageArray extends ArrayList {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        int iHeight,iWidth;
        int[][] image;

        public ImageArray(int w,int h){
            iHeight = h;
            iWidth = w;
            image = new int [iWidth][iHeight];
        }

        public void printArray(){
            for (int[] row : image) {
                for (int col : row) {
                    System.err.println(col);
                }
            }
        }

    }




    public static void main(String [] args)
       {
        ImageArray e = null;
        ImageArray im = null;
        int iWidth=0;
        int iHeight=0;
        BufferedImage biColouredImage = null;


          try
          {
             FileInputStream fileIn = new FileInputStream("i.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn);
             e = (ImageArray) in.readObject();
             in.close();
             fileIn.close();
          }catch(IOException i)
          {
             i.printStackTrace();
             return;
          }catch(ClassNotFoundException c)
          {
             System.out.println("class not found");
             c.printStackTrace();
             return;
          }

          try {
                FileInputStream ifile = new FileInputStream("i.ser");
                ObjectInputStream ois = new ObjectInputStream(ifile);
                im = (ImageArray) ois.readObject();
            } catch (Exception e1) {
                e1.printStackTrace();
            }

            try {
             BufferedImage biColouredImage_2 = new BufferedImage(im.iWidth, im.iHeight, BufferedImage.TYPE_BYTE_BINARY);

            for (int k = 0; k < iWidth; k++) {

            for (int l = 0; l < iHeight; l++) {
             biColouredImage_2.setRGB(k, l, im.image[k][l]);
             }

             }
             File file = new File("b.png");
             ImageIO.write(biColouredImage_2, "png", file);


             } catch (IOException ioe) {
             ioe.printStackTrace();
             }


        }

}

我收到如下错误....

class not found
java.lang.ClassNotFoundException: javaphotosecurity.ImageArray
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at java.io.ObjectInputStream.resolveClass(Unknown Source)
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at deserializeDemo.DeserializeDemo.main(DeserializeDemo.java:56)

0 个答案:

没有答案