Arrays and Images Post
Arrays and images post for AP CSA
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
public class ImageIOTest {
public static void main( String[] args ){
BufferedImage img = null; // buffer type
try {
// Name of file and directories
String name = "gojo";
String in = "images/";
String out = "images/tmp/";
// Either use URL or File for reading image using ImageIO
File imageFile = new File(in + name + ".png");
img = ImageIO.read(imageFile); // set buffer of image data
// ImageIO Image write to gif in Java
// Documentation https://docs.oracle.com/javase/tutorial/2d/images/index.html
ImageIO.write(img, "gif", new File(out + name + ".gif") ); // write buffer to gif
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Success");
}
}
ImageIOTest.main(null);
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;
public class Pics {
private final String inDir = "images/";
private final String outDir = "images/tmp/";
private String inFile;
private String resizedFile;
private String asciiFile;
private String grayScaledFile;
private String ext;
private long bytes;
private int width;
private int height;
public Pics(String name, String ext) {
this.ext = ext;
this.inFile = this.inDir + name + "." + ext;
this.resizedFile = this.outDir + name + "." + ext;
this.asciiFile = this.outDir + name + ".txt";
this.setStats();
}
public void setStats() {
BufferedImage img;
try {
Path path = Paths.get(this.inFile);
this.bytes = Files.size(path);
img = ImageIO.read(new File(this.inFile));
this.width = img.getWidth();
this.height = img.getHeight();
} catch (IOException e) {
}
}
public void printStats(String msg) {
System.out.println(msg + ": " + this.bytes + " " + this.width + "x" + this.height + " " + this.inFile);
}
public static BufferedImage convertToBufferedImage(Image img) {
BufferedImage bi = new BufferedImage(
img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = bi.createGraphics();
graphics2D.drawImage(img, 0, 0, null);
graphics2D.dispose();
return bi;
}
public void resize(int scale) {
BufferedImage img = null;
Image resizedImg = null;
int width = (int) (this.width * (scale/100.0) + 0.5);
int height = (int) (this.height * (scale/100.0) + 0.5);
try {
img = ImageIO.read(new File(this.inFile));
resizedImg = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
} catch (IOException e) {
return;
}
try {
ImageIO.write(convertToBufferedImage(resizedImg), this.ext, new File(resizedFile));
} catch (IOException e) {
return;
}
this.inFile = this.resizedFile;
this.setStats();
}
public void convertToAscii() {
BufferedImage img = null;
PrintWriter asciiPrt = null;
FileWriter asciiWrt = null;
try {
File file = new File(this.asciiFile);
Files.deleteIfExists(file.toPath());
} catch (IOException e) {
System.out.println("Delete File error: " + e);
}
try {
asciiPrt = new PrintWriter(asciiWrt = new FileWriter(this.asciiFile, true));
} catch (IOException e) {
System.out.println("ASCII out file create error: " + e);
}
try {
img = ImageIO.read(new File(this.inFile));
} catch (IOException e) {
}
for (int i = 0; i < img.getHeight(); i+=6) {
for (int j = 0; j < img.getWidth(); j+=3) {
Color col = new Color(img.getRGB(j, i));
double pixVal = (((col.getRed() * 0.30) + (col.getBlue() * 0.59) + (col
.getGreen() * 0.11)));
try {
asciiPrt.print(asciiChar(pixVal));
asciiPrt.flush();
asciiWrt.flush();
} catch (Exception ex) {
}
}
try {
asciiPrt.println("");
asciiPrt.flush();
asciiWrt.flush();
} catch (Exception ex) {
}
}
}
public void GrayScale() {
BufferedImage img = null;
try {
img = ImageIO.read(new File(this.inFile));
} catch (IOException e) {
}
for (int i = 0; i<this.height; i++) {
for (int j = 0; j<this.width; j++) {
int pixel = img.getRGB(j, i);
Color color = new Color(pixel, true);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int avg = (red+green+blue)/3;
Color colorGray = new Color(avg, avg, avg);
img.setRGB(j, i, colorGray.getRGB());
}
}
try{
ImageIO.write(img, "png", new File("images/tmp/" + "graygojo" + ".png") );
}catch(IOException e){
System.out.println(e);
}
}
public void GreenScale() {
BufferedImage img = null;
try {
img = ImageIO.read(new File(this.inFile));
} catch (IOException e) {
}
for (int i = 0; i<this.height; i++) {
for (int j = 0; j<this.width; j++) {
int pixel = img.getRGB(j, i);
Color color = new Color(pixel, true);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int avg = (green)/3;
Color colorGreen = new Color(avg, green, avg);
img.setRGB(j, i, colorGreen.getRGB());
}
}
try{
ImageIO.write(img, "png", new File("images/tmp/" + "greengojo" + ".png") );
}catch(IOException e){
System.out.println(e);
}
}
public void RedScale() {
BufferedImage img = null;
try {
img = ImageIO.read(new File(this.inFile));
} catch (IOException e) {
}
for (int i = 0; i<this.height; i++) {
for (int j = 0; j<this.width; j++) {
int pixel = img.getRGB(j, i);
Color color = new Color(pixel, true);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int avg = (red)/3;
Color colorRed = new Color(red, avg, avg);
img.setRGB(j, i, colorRed.getRGB());
}
}
try{
ImageIO.write(img, "png", new File("images/tmp/" + "redgojo" + ".png") );
}catch(IOException e){
System.out.println(e);
}
}
public void BlueScale() {
BufferedImage img = null;
try {
img = ImageIO.read(new File(this.inFile));
} catch (IOException e) {
}
for (int i = 0; i<this.height; i++) {
for (int j = 0; j<this.width; j++) {
int pixel = img.getRGB(j, i);
Color color = new Color(pixel, true);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int avg = (blue)/3;
Color colorBlue = new Color(avg, avg, blue);
img.setRGB(j, i, colorBlue.getRGB());
}
}
try{
ImageIO.write(img, "png", new File("images/tmp/" + "bluegojo" + ".png") );
}catch(IOException e){
System.out.println(e);
}
}
public void PurpleScale() {
BufferedImage img = null;
try {
img = ImageIO.read(new File(this.inFile));
} catch (IOException e) {
}
for (int i = 0; i<this.height; i++) {
for (int j = 0; j<this.width; j++) {
int pixel = img.getRGB(j, i);
Color color = new Color(pixel, true);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int avg = (blue)/3;
Color colorBlue = new Color(red, avg, blue);
img.setRGB(j, i, colorBlue.getRGB());
}
}
try{
ImageIO.write(img, "png", new File("images/tmp/" + "purplegojo" + ".png") );
}catch(IOException e){
System.out.println(e);
}
}
public void CyanScale() {
BufferedImage img = null;
try {
img = ImageIO.read(new File(this.inFile));
} catch (IOException e) {
}
for (int i = 0; i<this.height; i++) {
for (int j = 0; j<this.width; j++) {
int pixel = img.getRGB(j, i);
Color color = new Color(pixel, true);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int avg = (blue)/3;
Color colorBlue = new Color(avg, green, blue);
img.setRGB(j, i, colorBlue.getRGB());
}
}
try{
ImageIO.write(img, "png", new File("images/tmp/" + "cyangojo" + ".png") );
}catch(IOException e){
System.out.println(e);
}
}
public void YellowScale() {
BufferedImage img = null;
try {
img = ImageIO.read(new File(this.inFile));
} catch (IOException e) {
}
for (int i = 0; i<this.height; i++) {
for (int j = 0; j<this.width; j++) {
int pixel = img.getRGB(j, i);
Color color = new Color(pixel, true);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int avg = (blue)/3;
Color colorBlue = new Color(red, green, avg);
img.setRGB(j, i, colorBlue.getRGB());
}
}
try{
ImageIO.write(img, "png", new File("images/tmp/" + "yellowgojo" + ".png") );
}catch(IOException e){
System.out.println(e);
}
}
public void WhiteScale() {
BufferedImage img = null;
try {
img = ImageIO.read(new File(this.inFile));
} catch (IOException e) {
}
for (int i = 0; i<this.height; i++) {
for (int j = 0; j<this.width; j++) {
int pixel = img.getRGB(j, i);
Color color = new Color(pixel, true);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int avg = (blue)/3;
Color colorBlue = new Color(red, green, blue);
img.setRGB(j, i, colorBlue.getRGB());
}
}
try{
ImageIO.write(img, "png", new File("images/tmp/" + "whitegojo" + ".png") );
}catch(IOException e){
System.out.println(e);
}
}
public String asciiChar(double g) {
String str = " ";
if (g >= 240) {
str = " ";
} else if (g >= 210) {
str = ".";
} else if (g >= 190) {
str = "*";
} else if (g >= 170) {
str = "+";
} else if (g >= 120) {
str = "^";
} else if (g >= 110) {
str = "&";
} else if (g >= 80) {
str = "8";
} else if (g >= 60) {
str = "#";
} else {
str = "@";
}
return str;
}
public static void main(String[] args) throws IOException {
Pics gojo = new Pics("gojo", "png");
gojo.GrayScale();
gojo.GreenScale();
gojo.RedScale();
gojo.BlueScale();
gojo.PurpleScale();
gojo.CyanScale();
gojo.YellowScale();
gojo.WhiteScale();
gojo.printStats("Original");
gojo.resize(33);
gojo.printStats("Scaled");
gojo.convertToAscii();
}
}
Pics.main(null);