【发布时间】:2015-01-17 23:59:14
【问题描述】:
我想要做的是使用 for 循环来增加图像的亮度。我正在尝试使用 for 循环来查找每个像素并将亮度更改一定量。我想创建一个数组并将图像保存在该数组中,然后它将在同一文件夹中打印出新图像。同样在我的代码中,我制作了一种反射方法,它采用每个像素并垂直反射图像。要运行该程序,用户必须在 CMD 中键入图像名称和输出文件的名称。这是我的完整代码:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
public class ImageProcessor {
public static void main(String[] args){
if(args.length < 3){
System.out.println("Not enough arguments");
System.exit(-1);
}
String function = args[0];
if (function.equals("-reflectV"))
{
String inputFileName = args[1];
String outputFileName = args[2];
int [][] imageArr = readGrayscaleImage(inputFileName);
int [][] reflectedArr = reflectV(imageArr);
writeGrayscaleImage(outputFileName, reflectedArr);
}
else if (function.equals("-ascii")){
String inputFileName = args[1];
String outputFileName = args[2];
int [][] imageArr = readGrayscaleImage(inputFileName);
int [][] reflectedArr = reflectV(imageArr);
try{
PrintStream output = new PrintStream(new File("output.txt"));
}
catch (java.io.FileNotFoundException ex) {
System.out.println("Error: File Not Found");
System.exit(-1);
}
}
else if (function.equals("-adjustBrightness")){
String amount = args[1];
int a = Integer.parseInt(amount);
System.out.print(a)
}
public static int[][] reflectV(int[][] arr){
int[][] reflected = new int[arr.length][arr[0].length];
for(int i=0; i< arr.length; i++){
for(int j=0; j<arr[i].length;j++){
reflected[i][j] = arr[i][arr[i].length-1-j];
}
}
return reflected;
}
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
}
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);
}
}
}
【问题讨论】:
-
我不太确定如何在我的代码中实现该代码中的某些内容。我想要做的是使用 for 循环来查找每个像素,然后调整该像素的亮度(有点像我用反射方法所做的)。两个问题是我不确定如何处理每个像素来增加或减少亮度,并且我不确定调用该方法时该怎么做。当我调用反射方法时,我使用了两个数组,但是对于亮度方法,我必须做一些稍微不同的事情。
标签: java arrays image loops brightness