java codes

This is to convert any picture into byte format. You can convert jpg or png format into byte format and save to database if required using Java. This script also give image property like actual image width and height in pixels. Yuu can get picture properties with “width x height” format in a string format.

Convert Picture into Byte, Get Image Width and Height using Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.main;
 
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
 
public class ImageUtil {
 
	public static String imageWH() {
		File imageFile = new File ("C:\test50.png");
		FileInputStream fisLocal = new FileInputStream(imageFile);
		byte[] imageByteLocal = IOUtils.toByteArray(fisLocal);
 
		//converting file format
		FileInputStream fis = new FileInputStream(imageFile);
		//Get Image height and width
		BufferedImage bimg = ImageIO.read(fis);
		int width          = bimg.getWidth();
		int height         = bimg.getHeight();
		return (width + "x" + height);
	}
}

Leave a comment