Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description In this workshop, you will investigate digital image formats. Specif

ID: 3639159 • Letter: D

Question

Description
In this workshop, you will investigate digital image formats. Specifically, you will create a program, or a
set of detailed instructions (with special permission from the instructor), that can be used to detect the
format of an image from a list of possible image types. You may work individually or with one other
student.
Program Option
• Create a program that takes a single argument consisting of a filename
• The program should open and read the file and determine the type of image data contained in the file,
and print out the name of the file, the type of image file found and the image dimensions, such as:
imagefile1 JPEG 300x200
imagefile2 GIF 256x256
imagefile3 PNG 800x600
etc.
• A test run with each of the sample images provided, demonstrating the correct image file type
determination of each
Detailed Instructions Option
• Create a document that contains instructions on determined the image type of a file
• Instructions should include steps on:
• opening the file using some existing, non-imaging, software tool such as Wordpad or vi or other textbased program, and
• viewing and interpreting the data to determine the type of image file opened
• A list of each of the sample images provided together with the image file type, and dimensions if
possible, for each

Detects the following image formats:
JPEG, GIF, PNG, TIFF, BMP, PPM, PGM, PBM, EPS, PICT

I am expecting the code in JAVA

Explanation / Answer

import java.io.*;
import java.awt.image.*;
import java.awt.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.util.*;

public class ImageTest {
static String fileName;
static Dimension result;

public static void main(String[] args) {

if (args.length > 0) {
ImageTest it = new ImageTest(args[0]);

} else {
System.err.println("Argument" + " must pass filename");
}

}

public ImageTest(String filename) {
try {
InputStream is = new FileInputStream(filename);
String formatName = getFormatName(is);
System.out.println("Format Type : " + formatName);
System.out.println("Format readable? : "
+ canReadFormat(formatName));

if (canReadFormat(formatName)) {
System.out.println("Width : " + result.width + " Height : " + result.height);
}

} catch (Exception e) {
System.out.println(e.getMessage());
}

}

// Returns true if the specified format name can be read
public static boolean canReadFormat(String formatName) {
Iterator iter = ImageIO.getImageReadersByFormatName(formatName);
return iter.hasNext();
}

public static String getFormatFromStream(InputStream is) {
return getFormatName(is);
}

private static String getFormatName(Object o) {
try {
// Create an image input stream on the image
ImageInputStream iis = ImageIO.createImageInputStream(o);

// Find all image readers that recognize the image format
Iterator iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
// No readers found
return null;
}

// Use the first reader
ImageReader reader = (ImageReader) iter.next();
try {
ImageInputStream stream = iis;// new ImageInputStream(iis);
reader.setInput(stream);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
result = new Dimension(width, height);
} catch (IOException e) {
// log(e.getMessage());
} finally {
reader.dispose();
}

// Close stream
iis.close();

// Return the format name
return reader.getFormatName();
} catch (IOException e) {
}
// The image could not be read
return null;
}

}