LaoCooon LaoCooon
关注数: 137 粉丝数: 565 发帖数: 12,384 关注贴吧数: 207
ImageProcessor.java // Initial source-code for ImageProcessor.java // Given as part of Assignment #6, CSC 110, UVic, Fall 2014// Originally created by Mike Zastre, last edited by Melanie Tory, Nov. 2014import java.awt.Color;import java.awt.image.BufferedImage;import java.io.*;import javax.imageio.ImageIO;public class ImageProcessor { // THIS METHOD MAY BE CALLED, BUT MUST NOT BE MODIFIED! // This method reads an image file. // expects one parameter: a filename of an image file to be read // returns a 2D array of ints representing grayscale values in the input image public static int[][] readGrayscaleImage(String filename) { int [][] result = null; //create the array try { File imageFile = new File(filename); //create the file BufferedImage image = ImageIO.read(imageFile); int height = image.getHeight(); int width = image.getWidth(); result = new int[height][width]; //read each pixel value for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int rgb = image.getRGB(x, y); result[y][x] = rgb & 0xff; } } } catch (IOException ioe) { System.err.println("Problems reading file named " + filename); System.exit(-1); } return result; //once we're done filling it, return the new array } // THIS METHOD MAY BE CALLED, BUT MUST NOT BE MODIFIED! // This method creates an output image based on an array of ints and writes it to a file. // expects two parameters: a filename for the image file that will be created // and a 2D array of ints that will be converted into the image public static void writeGrayscaleImage(String filename, int[][] array) { int width = array[0].length; int height = array.length; try { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //create the image //set all its pixel values based on values in the input array for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int rgb = array[y][x]; rgb |= rgb << 8; rgb |= rgb << 16; image.setRGB(x, y, rgb); } } //write the image to a file File imageFile = new File(filename); ImageIO.write(image, "jpg", imageFile); } catch (IOException ioe) { System.err.println("Problems writing file named " + filename); System.exit(-1); } } public static void ascii(String filename,int array[][]) throws FileNotFoundException{ int height = array.length; int width = array[0].length; PrintStream output=new PrintStream(new File(filename)); output.println("设置成不自动换行,字体大小设置为1或2"); for(int y=0;y<height;y++){ for(int x=0;x<width;x++) { if (array[y][x]>=0 && array[y][x]<=25) output.print("M"); if (array[y][x]>=26 && array[y][x]<=50) output.print("$"); if (array[y][x]>=51 && array[y][x]<=76) output.print("o"); if (array[y][x]>=77 && array[y][x]<=102) output.print("|"); if (array[y][x]>=103 && array[y][x]<=127) output.print("*"); if (array[y][x]>=128 & array[y][x]<=152) output.print(":"); if (array[y][x]>=153 && array[y][x]<=178) output.print("="); if (array[y][x]>=179 && array[y][x]<=204) output.print("\\"); if (array[y][x]>=205 && array[y][x]<=230) output.print("."); if (array[y][x]>=231 & array[y][x]<=255) output.print(" "); if (x==width-1) output.println(""); } } } public static void reflectV(int array[][]) { int height = array.length; int width = array[0].length; int rgb; for(int y=0;y<height;y++){ for(int x=0;x<(width/2);x++) { rgb=array[y][x]; array[y][x]=array[y][width-x-1]; array[y][width-x-1]=rgb; } } } public static void reflectH(int array[][]) { int height = array.length; int width = array[0].length; int rgb; for(int y=0;y<height/2;y++){ for(int x=0;x<(width);x++) { rgb=array[y][x]; array[y][x]=array[height-y-1][x]; array[height-y-1][x]=rgb; } } } public static int[][] tile(int array[][],int hz,int vt) { int height = array.length; int width = array[0].length; int [][] result = null; height*=vt; width*=hz; result = new int[height][width]; for(int y=0;y<height;y++){ for(int x=0;x<width;x++) { result[y][x]=array[y%array.length][x%array[0].length]; } } return result; } public static void adjustBrightness(int array[][],int amount) { int height = array.length; int width = array[0].length; int rgb; for(int y=0;y<height;y++){ for(int x=0;x<(width);x++) { rgb=array[y][x]+amount; if(rgb<0) rgb=0; if(rgb>255) rgb=255; array[y][x]=rgb; } } } public static void main(String[] args) throws FileNotFoundException{ int [][] result = null; int [][] result2 =null; if(args[0].equals("-ascii")){ result=readGrayscaleImage(args[1]); ascii(args[2],result); } if(args[0].equals("-reflectV")){ result=readGrayscaleImage(args[1]); reflectV(result); writeGrayscaleImage(args[2],result); } if(args[0].equals("-reflectH")){ result=readGrayscaleImage(args[1]); reflectH(result); writeGrayscaleImage(args[2],result); } if(args[0].equals("-tile")){ result=readGrayscaleImage(args[3]); result2= tile(result,Integer.parseInt(args[1]),Integer.parseInt(args[2])); writeGrayscaleImage(args[4],result2); } if(args[0].equals("-adjustBrightness")){ result=readGrayscaleImage(args[2]); adjustBrightness(result,Integer.parseInt(args[1])); writeGrayscaleImage(args[3],result); } }}
首页 1 2 3 4 下一页