【问题标题】:encrypted byte array to image加密字节数组到图像
【发布时间】:2014-11-10 20:07:11
【问题描述】:

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

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

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

【问题讨论】:

    标签: java arrays opencv encryption


    【解决方案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());
    

    【讨论】:

    • 感谢您的快速回复和代码。我想将加密数据(jpg)发送到服务器,并且只在服务器上解密字节。您的方法建议在客户端本身进行解密。我想用加密的字节创建 jpg 文件。请帮忙。我为糟糕的英语道歉。
    • @user987743 服务器端如何接收字节流?您能否上传您的代码或至少一些片段?如果长期使用gist.github.com
    • gist.github.com/anonymous/4d5baacde866ef4ad450 在此处添加了代码。正如我已经提到的,它使用 opencv 和 ffmpeg。该程序运行良好,我只是删除了加密行。如果我提供一个 jpg 文件来 ffinput 它可以工作。
    • 我想那是客户端。我没有关于你的服务器的信息。我对您如何将数据发送到服务器以及它如何接收数据感兴趣。你的服务器是什么?
    • 你是对的。这是我的客户端代码。我以为我只需要修改客户端,因此单独发送该代码。无论如何,我的服务器代码在这里 gist.github.com/anonymous/4215593459a83e730c1e 。服务器端适用于所有 jpgs
    猜你喜欢
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多