Write a program in java that: 1. Reads the image data file “original.txt” into y
ID: 3806578 • Letter: W
Question
Write a program in java that:
1. Reads the image data file “original.txt” into your program, (file contains the following numbers):
1122333444
0011223334
0000011223
5000000001
5550000000
5555550000
5555555555
then output another file with the image data converted to ASCII symbols that represent the darkness of each pixel. a. You must create a class called “Image” to represent the image
b. This class should accept image data in the form of a 2-dimentional integer matrix.
**This class should not import Scanner, System.io, File, etc. (after all this data matrix might come from another source, another object, who knows). **
c. You should create a second class, called “pictureOutput” that handles file and console input and output, along with conversion of data to a Java array that can be passed to Image.
-In addition to reading in a data file and converting it to a Java array, pictureOutput should also be able to accept an Image object and write its contents to disk.
-In addition to simply writing the raw data, pictureOutput should be able to convert that data to ASCII art, and write the ASCII version of the image to disk. Use the following to handle this conversion:
0 = “space”
1 = .
2 = :
3 = o
4 = O
5 = $
2. Create a utility Object – similar to Math – called “pictureEdit” that contains methods for working with and manipulating images
a. pictureEdit must accept Image objects as method parameters as its primary means of receiving image data. No passing arrays! Use proper object-oriented style! b. pictureEdit must also implement at least two methods in some way related to manipulating images. This may include:
i. Rotating the image 90 degrees or 180 degrees
ii. Flipping the image vertically or horizontally or diagonally
iii. Cropping the image
1. I imagine something like crop(myImage, 0, 1,2,0) which would remove 0 rows from the top, 1 from the left, 2 from the bottom, and 0 from the left. Your call on specifics, though.
iv. Adding a border to the image
1. surround (myImage, 5, 2) would add a border of “darkness” value 5 that is 2 “pixels” wide around the entire image. Your call on specifics, though.
v. Whatever else you feel like doing: image compression, combining two images together, darkening or lightening the image by changing the values of the “pixels”, or whatever you feel like doing. It’s up to you;
c. pictureEdit must contain at least one method that is declared using the “static” keyword, (much as Math.sqrt() is declared statically) and you must demonstrate proper use of it in main().
3. All 3 objects must be contained in a package called “EasyPicture”
4. In this package, you must also include a class called “picTest” that contains main() and demonstrates the successful completions of the requirements of this assignment. The code in main() should:
a. Read in the data from “original.txt”
b. Create a Java array from that data
c. Create an Image object from that array
d. Convert that Image object to ASCII art
e. Write the ASCII version to disk
f. Perform at least 2 manipulations on that image i. (Please only perform these manipulations on the data found in “original.txt”, and not on Images that have already been manipulated.)
Explanation / Answer
Image.java:
public class Image {
int[][] data;
public Image(int[][] data) {
this.data = data;
}
char[][] getAsciiArt() {
char[][] result = new char[data.length][10];
for(int i=0; i < result.length; i++) {
for(int j=0; j< result[i].length; j++) {
if(data[i][j] == 0)
result[i][j] = ' ';
else if(data[i][j] == 1)
result[i][j] = '.';
else if(data[i][j] == 2)
result[i][j] = ':';
else if(data[i][j] == 3)
result[i][j] = 'o';
else if(data[i][j] == 4)
result[i][j] = 'O';
else if(data[i][j] == 5)
result[i][j] = '$';
}
}
return result;
}
public int[][] getData() {
return data;
}
public int getPixel(int i, int j) {
return data[i][j];
}
public void printPicture() {
for(int i=0; i<data.length; i++) {
for(int k: data[i]) {
System.out.print(k);
}
System.out.println();
}
}
}
pictureOutput.java:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class pictureOutput {
public static int[][] readData() throws IOException {
BufferedReader reader =new BufferedReader(new FileReader("input.txt"));
int data[][];
String line;
int no_of_lines = 0;
while((line = reader.readLine()) != null) {
no_of_lines++;
}
data = new int[no_of_lines][10];
reader.close();
reader =new BufferedReader(new FileReader("input.txt"));
int n=0;
while((line = reader.readLine()) != null) {
char tokens[] = line.toCharArray();
int i=0;
for(char c: tokens) {
data[n][i++] = c-'0';
}
n++;
}
reader.close();
return data;
}
public static void writeData(char[][] asciiArt) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter("ascii_" + "input.txt"));
for(int i=0; i < asciiArt.length; i++) {
for(int j=0; j< asciiArt[i].length; j++) {
writer.append(asciiArt[i][j]);
}
writer.append(" ");
}
writer.close();
}
public static void main(String[] args) throws IOException {
int data[][] = readData();
Image image = new Image(data);
System.out.println("Image after readin test file: ");
image.printPicture();
char[][] asciiArt = image.getAsciiArt();
writeData(asciiArt);
System.out.println(" Wrote ascii data to the file");
image = pictureEdit.flipHorizontally(image);
System.out.println(" After flipping horizontally");
image.printPicture();
image = pictureEdit.flipVertically(image);
System.out.println(" After flipping vertically");
image.printPicture();
image = pictureEdit.cropImage(image,0,1,1,0);
System.out.println(" After Cropping 0,1,1,0");
image.printPicture();
image = pictureEdit.surround(image, 3, 1);
System.out.println(" After Surrounding 0,1,1,0");
image.printPicture();
image = pictureEdit.rotateImageBy90(image);
System.out.println(" After rotating image by 90");
image.printPicture();
}
}
pictureEdit.java:
public class pictureEdit {
public static Image flipHorizontally(Image image) {
int[][] data = image.getData();
for (int i = 0; i < (data.length / 2); i++) {
int[] temp = data[i];
data[i] = data[data.length - i - 1];
data[data.length - i - 1] = temp;
}
return new Image(data);
}
public static Image flipVertically(Image image) {
int[][] data = image.getData();
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < (data[i].length / 2); j++) {
int temp = data[i][j];
data[i][j] = data[i][data[i].length - j - 1];
data[i][data[i].length - j - 1] = temp;
}
}
return new Image(data);
}
public static Image cropImage(Image image, int topRow, int leftCol, int bottomRow, int rightCol) {
int[][] data = image.getData();
int[][] result = new int[data.length - topRow - bottomRow][data[0].length - leftCol - rightCol];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
result[i][j] = data[topRow + i][leftCol + j];
}
}
return new Image(result);
}
public static Image surround(Image image, int value, int thickness){
int[][] data = image.getData();
int[][] result = new int[data.length + 2*thickness][data[0].length + 2*thickness];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
result[i][j] = value;
}
}
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
result[i + thickness][j + thickness] = data[i][j];
}
}
return new Image(result);
}
public static Image rotateImageBy90(Image image) {
int[][] m = image.getData();
int[][] result = new int[m[0].length][m.length];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[0].length; j++) {
result[j][i]= m[i][j];
}
}
return new Image(result);
}
}
Sample Output:
Image after readin test file:
1122333444
0011223334
0000011223
5000000001
5550000000
5555550000
5555555555
Wrote ascii data to the file
After flipping horizontally
5555555555
5555550000
5550000000
5000000001
0000011223
0011223334
1122333444
After flipping vertically
5555555555
0000555555
0000000555
1000000005
3221100000
4333221100
4443332211
After Cropping 0,1,1,0
555555555
000555555
000000555
000000005
221100000
333221100
After Surrounding 0,1,1,0
33333333333
35555555553
30005555553
30000005553
30000000053
32211000003
33332211003
33333333333
After rotating image by 90
33333333
35000233
35000233
35000133
35500123
35500023
35500013
35550013
35550003
35555003
33333333
Ascii file:
..::oooOOO
..::oooO
..::o
$ .
$$$
$$$$$$
$$$$$$$$$$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.