0

What I'm trying to do is use for loops to increase the brightness of an image. I'm trying to use for loops to find each pixel and change the brightness by a certain amount. I want to create an array and save the image in that array and then it will print out the new image in the same folder. Also in my code I've made a reflection method which takes each pixel and reflects the image vertically. To run the program the user will have to type the image name and the name of the output file in CMD. Here's my entire code:

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);
        }
    }
}
David Rolfe
  • 163
  • 2
  • 10
  • Check this http://stackoverflow.com/questions/10208255/how-to-increase-decrease-brightness-of-image-using-jslider-in-java – happyvirus Nov 20 '14 at 07:49
  • I'm not quite sure how to implement certain things in that code in my code. What I want to do is use for loops to find every single pixel and then adjust the brightness for that pixel (kind of like what I did with my reflect method). The 2 problems are that I'm not sure what to do with each pixel to increase or decrease the brightness and I'm not sure what to do when I call the method. When I called my reflect method I used two array's however for the brightness method I have to do things slightly different. – David Rolfe Nov 20 '14 at 07:54

1 Answers1

0

I hope this code will help!

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Color;

public class Brighter
{
     public static void main(String args[])
     {
          BufferedImage img=null;
          try
          {
             img=ImageIO.read(new File("file_path")); // read image
             for(int i=0;i<img.getHeight();i++)
             {
                for(int j=0;j<img.getWidth();j++)
                {
                    int  color =  image.getRGB(j,i);
                    int  alpha = (color & 0x00ff0000) >> 24; 
                    int  red   = (color & 0x00ff0000) >> 16;
                    int  green = (color & 0x0000ff00) >> 8;
                    int  blue  =  color & 0x000000ff;
                    Color c=new Color(red, green, blue);
                    c=c.brighter();
                    red=c.getRed();
                    green=c.getGreen();
                    blue=c.getBlue();
                    color=(alpha<<24) | (red<<16) | (green<<8) & (blue);
                    img.setRGB(j,i,color);
                }
             }
             ImageIO.write(img,"jpg",new File("output.jpg"));
          }catch(IOException e){}
     }
}
  • Some explanation will be helpful – U13-Forward Feb 16 '20 at 08:47
  • Basically what i did was that I took the color of the pixel and used bitwise operation to get the amount of red, green, and blue color in it. Then I used it to form a new "Color" class object and then used the method "brighter()" to get the brighter version of that color, then I used the in-built class methods "getRed()", "getBlue()", and "getGreen()" for finding the corresponding values and then using Bitwise operation again to get the required color! This Code is not that messy though I think! – Code_Master Feb 16 '20 at 08:49
  • Add it to the post please – U13-Forward Feb 17 '20 at 01:56