2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

How to resize an image without using external api in java and by maintaining the image resolution

My project requirement is once user upload any image, from server side i need to check the image size/resolution and if the image it's greater than the expected resolution then i need to shrink the image to a specific resolution without changing image quality. I mean the image quality should be good enough to recognize things. And my problem is i cannot using any external api for shrinking the image. I have to using only Java code. So is there any good way to do so?


Thanks in advance

userimage

Yes i got the scenario where i also got the exception like this after upload a PNG file and i did updated my code as given my previous ans:

the exception i got as below:

java.lang.IllegalArgumentException: Unknown image type 0  at java.awt.image.BufferedImage.<init>(BufferedImage.java:490)  at 

And the solution i did for this as below:

private static byte[] resizeImage(byte[] imageContent, String imageFileName) {
	  ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageContent);
	  try {
			 BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);
			 int height = bufferedImage.getHeight();
			 int width = bufferedImage.getWidth();
			 int defaultLength = 1440;
			 int defaultWidth = 900;
			 if(height > defaultLength || width > defaultWidth){
				   BufferedImage resizedImage = new BufferedImage(defaultLength, defaultWidth, bufferedImage.getType() == 0 ? 5 : bufferedImage.getType());
				   Graphics2D g = resizedImage.createGraphics();
				   g.drawImage(bufferedImage, 0, 0, defaultLength, defaultWidth, null);
				   g.dispose();
				   ByteArrayOutputStream baos = new ByteArrayOutputStream();
				   ImageIO.write(resizedImage, imageFileName.split(Pattern.quote("."))[1], baos);
				   baos.flush();
				   return baos.toByteArray();
			 }else{
				   return imageContent;
			 }
	  } catch (IOException e) {
			 e.printStackTrace();
			 return imageContent;
	  }
}

hope this will resolve your issue as it's resolved mine too.

userimage

I do had same kind of requirement once, so i can provide the code snippet for the problem you facing. hope this will help you for resolving your issue.

private static byte[] resizeImage(byte[] imageContent, String imageFileName) {
	  ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageContent);
	  try {
			 BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);
			 int height = bufferedImage.getHeight();
			 int width = bufferedImage.getWidth();
			 int defaultLength = 1440;
			 int defaultWidth = 900;
			 if(height > defaultLength || width > defaultWidth){
				   BufferedImage resizedImage = new BufferedImage(defaultLength, defaultWidth, bufferedImage.getType());
				   Graphics2D g = resizedImage.createGraphics();
				   g.drawImage(bufferedImage, 0, 0, defaultLength, defaultWidth, null);
				   g.dispose();
				   ByteArrayOutputStream baos = new ByteArrayOutputStream();
				   ImageIO.write(resizedImage, imageFileName.split(Pattern.quote("."))[1], baos);
				   baos.flush();
				   return baos.toByteArray();
			 }else{
				   return imageContent;
			 }
	  } catch (IOException e) {
			 e.printStackTrace();
			 return imageContent;
	  }
}
Answer is