Original content

Protected by Copyscape Plagiarism Checker
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, February 15, 2015

Image resize in java - pass image as byte code and write the output byte code as image file

In this code, variable “byteImageValue” is the actual image byte code.

        byte[] byteResizedImageValue;
        try {
              ByteArrayInputStream inputStream = new ByteArrayInputStream(byteImageValue);
                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
                     byte[] imageInByte = null
                     BufferedImage bufferedImage = ImageIO.read(inputStream);
                     int intResizeWidth = bufferedImage.getWidth()+25;
                     int intResizedHeight = bufferedImage.getHeight()+10;
                     BufferedImage resizedImage = new BufferedImage(intResizeWidth, intResizedHeight, BufferedImage.TYPE_INT_ARGB);
                     Graphics g = resizedImage.getGraphics();
                    
                     g.drawImage(bufferedImage, 0, 0, intResizeWidth, intResizedHeight, null);
               g.dispose();
                     ImageIO.write(resizedImage, "PNG", baos );
               baos.flush();
               imageInByte = baos.toByteArray();
               baos.close();
               byteResizedImageValue = imageInByte; 
              } catch (IOException objImageIOException) {
                     // TODO Auto-generated catch block
                     throw new ScriptException(generalErrorCode,
                    "Invalid image." + objImageIOException.getMessage());
              }