HomeCodeHow to Convert Picture into Byte, Get Image Width and Height using...

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

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

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

Related: How to Take Screenshot and Save to MSSQL Database using Java and Selenium Webriver


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);
}
}

Disclosure: Mashtips is supported by its audience. As an Amazon Associate I earn from qualifying purchases.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

More from Author