Posts

Showing posts from 2019

Program to convert base64 to Image and vice versa

Code snippet: This method decodes the base64 string into the bytes and converts it into an image .      * Note: this method expects input to be an encoded Image in base64 format. It can't work with other other files encoded in base64 format ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(base64data)); BufferedImage img = ImageIO.read(bis); bis.close(); This method encodes the PNG image to base64 format ByteArrayOutputStream output = new ByteArrayOutputStream(); ImageIO.write(image, "PNG", output); String encodedString = Base64.getEncoder().encodeToString(output.toByteArray()); output.close();

Program to convert image byte array to PDF

Simple sample program to convert image byte array to PDF         //input path. path where byte array is saved         String pathOfByteArrayFile = "/home/inputByteArray.txt";         //output path. path where rendered you need to save . Output PDF should render properly         String outputPDFPath = "/home/output.pdf";         try         {             File file = new File(pathOfByteArrayFile);             FileOutputStream fout = new FileOutputStream(outputPDFPath);             byte[] bArray = Base64.decode(FileUtils.readFileToByteArray(file));             fout.write(bArray);             fout.close();             assertFalse(false);         }         catch (Exception e)         {             //your logic for failure         }