加密的字节数组到图像

时间:2014-09-16 22:40:26

标签: java arrays opencv encryption

我必须从摄像头捕获视频,对其进行加密并将其发送到服务器。 我已经使用OpenCV逐帧捕获。我将帧转换为字节数组并使用AES加密。我得到一个字节数组作为AES的输出。

我尝试使用Byte Array to Image File从加密内容(字节数组)创建图像 但是ImageIO.read给出了null。原因可能是因为这个原因  ImageIO.read returns NULL, with no errors

如何从字节数组创建图像(最好是jpg)?

1 个答案:

答案 0 :(得分:1)

假设您的客户端类在服务器类中发送加密的字节数组,解密字节数组,然后从解密的字节数组重构图像。

ByteArrayInputStream dis = new ByteArrayInputStream(your_EncryptedBytes_array);


//Call Your decrypt method here.

ByteArrayInputStream bis = new ByteArrayInputStream(your_DecryptedBytes_array);

Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg");



/*ImageIO is a class containing static methods for locating ImageReaders
  and ImageWriters, and performing simple encoding and decoding. 
  Method  returns an Iterator containing all currently registeredImageReaders that 
  claim to be able to decode the named format */
 ImageReader reader = (ImageReader) readers.next();
        Object source = bis; 

        ImageInputStream iis = ImageIO.createImageInputStream(source); 
        reader.setInput(iis, true);
        ImageReadParam param = reader.getDefaultReadParam();

        Image image = reader.read(0, param);
        //got an image file

        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
        //bufferedImage is the RenderedImage to be written

        Graphics2D g2 = bufferedImage.createGraphics();
        g2.drawImage(image, null, null);

        File imageFile = new File("INERT_LOCATION");
        ImageIO.write(bufferedImage, "jpg", imageFile);

        System.out.println(imageFile.getPath());