Reputation: 45
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?
Upvotes: 0
Views: 786
Reputation: 903
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());
Upvotes: 1