0

I have to capture video from camera,encrypt it and send it to the Server. I have used OpenCV to capture frame by frame. I converted frame to byte array and encrypted using AES.I get a byte array as output from AES.

I tried to create image from encrypted content(byte array) using Byte Array to Image File But ImageIO.read gives null. The reason could be because of this ImageIO.read returns NULL, with no errors

How to create an image(preferrably jpg) from the byte array?

Community
  • 1
  • 1
user987743
  • 45
  • 1
  • 6

1 Answers1

1

Assuming that your client class sends the encrypted bytes arrays, in your server class, decrypt the byte array and then reconstruct your image from the decrypted byte array.

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());
Y. Leonce Eyog
  • 883
  • 2
  • 13
  • 29
  • thanks for your fast reply and code. I want to send encrypted data(jpg) to the Server and only decrypt bytes at the Server. Your method suggests decrypting at client end itself. I want to create jpg file with the encrypted bytes. Please help. I apologize for the bad english. – user987743 Sep 17 '14 at 00:31
  • @user987743 How do you receive the byte stream on the server side? Could you please upload your codes or at least some fragments?. If it's long use http://gist.github.com – Y. Leonce Eyog Sep 17 '14 at 01:03
  • https://gist.github.com/anonymous/4d5baacde866ef4ad450 added code here. As i already mentioned it uses opencv and ffmpeg. the program works fine i just remove the line with encryption. If i supply a jpg file to ffinput it works. – user987743 Sep 17 '14 at 01:12
  • I suppose that was the client side. I have no info on your server. I'm interested on how you send data to the server and how it receives it. What is your server? – Y. Leonce Eyog Sep 17 '14 at 02:15
  • You are correct.that is my client side of code. I thought i have to modify client side only and hence send that code alone. Anyways my server code is here https://gist.github.com/anonymous/4215593459a83e730c1e . Server side works for all jpgs – user987743 Sep 17 '14 at 03:42
  • What happens if you call the decryption code in the server class? – Y. Leonce Eyog Sep 17 '14 at 04:43
  • I am not able to send anything from client as I am not able to get jpg from encrypted array. Client itself I am getting null pointer exception due to that. – user987743 Sep 17 '14 at 05:55
  • I'd like to see the classes full if possible as I thought the algorithm was such that from the client: you read from the image, put it in a byte array and encrypt it. From the server I expected you'd: decrypt the byte array and form the image, display it and finally repaint it. Could you reedit your question and explain better what you did with the codes included? – Y. Leonce Eyog Sep 17 '14 at 06:25