I am working on a project that converts images to grayscale and I want to change the Source file to be user input.
this the code i am working with
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class GrayScale{
public static void main(String args[])throws IOException{
BufferedImage img = null;
File f = null;
try{
f = new File("F:\\test\\test.jpg");
img = ImageIO.read(f);
}catch(IOException e){
System.out.println(e);
}
int width = img.getWidth();
int height = img.getHeight();
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
int p = img.getRGB(x,y);
int a = (p>>24)&0xff;
int r = (p>>16)&0xff;
int g = (p>>8)&0xff;
int b = p&0xff;
int avg = (r+g+b)/3;
p = (a<<24) | (avg<<16) | (avg<<8) | avg;
img.setRGB(x, y, p);
}
}
try{
f = new File("F:\\test\\Output.jpg");
ImageIO.write(img, "jpg", f);
}catch(IOException e){
System.out.println(e);
}
}
}
So I want a way to change the try catch method for input and output to be specified by the user not standard
Thanks in Advance
Related
I was ready through Wikipedia about Pixel, I found out that a pixel can be rendered in different shapes, and not necessarily square shape.
How can I create image with different shaped pixels in Java?
Below is how I create an Image with Random pixels
/**
* File: RandomImage.java
*
* Description:
* Create a random color image.
*
* #author King Amada
* Date: 07-17-20148 tue
*/
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class RandomImage{
public static void main(String args[])throws IOException{
//image dimension
int width = 640;
int height = 320;
//create buffered image object img
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//file object
File f = null;
//create random image pixel by pixel
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
int a = (int)(Math.random()*256); //alpha
int r = (int)(Math.random()*256); //red
int g = (int)(Math.random()*256); //green
int b = (int)(Math.random()*256); //blue
int p = (a<<24) | (r<<16) | (g<<8) | b; //pixel
img.setRGB(x, y, p);
}
}
//write image
try{
f = new File("D:\\Image\\Output.png");
ImageIO.write(img, "png", f);
}catch(IOException e){
System.out.println("Error: " + e);
}
}//main() ends here
}//class ends here
When i run the code, the file shows up and it disappears and this error shows up. It disappears in the middle of the execution of the code.
The Java folder only has Sample.jpg, ColorGet.java and ColorGet.class
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.lang.*;
public class ColorGet{
public static void main(String args[])throws IOException{
BufferedImage img = null;
File f = null;
File m = null;
int c = 0;
try{
f = new File("C:\\Users\\Lance Dean\\Desktop\\Java\\Sample.jpg");
img = ImageIO.read(f);
}catch(IOException e){
System.out.println(e);
}
int width = img.getWidth();
int height = img.getHeight();
for (int i=0; i < width;i++){
for (int j=0; j < height;j++){
int p = img.getRGB(i,j);
int r = (p>>16) & 0xff;
int g = (p>>8) & 0xff;
int b = p & 0xff;
c++;
System.out.println(c);
int a = 4 * (int)(Math.floor(255/4));
int x = 4 * (int)(Math.floor((double)(r/4)));
int y = 4 * (int)(Math.floor((double)(g/4)));
int z = 4 * (int)(Math.floor((double)(b/4)));
p = (a<<24) | (x<<16) | (y<<8) | z;
img.setRGB(i, j, p);
try{
m = new File("C:\\Users\\Lance Dean\\Desktop\\Java\\End.jpg");
if (!m.canRead()){
m.setReadable(true);
}
ImageIO.write(img, "jpg", m);
}catch(IOException e){
System.out.println(e);
}
}
}
}
}
There is a lot of IO operations in your code. Let's say image is 400x300, then you have opened and write the file for 12k times. As IO ops might be blocking, hence you might face access denied issue if the file is blocked by previous operation. It's better to do one write only at the end.
public static void main(String args[]) throws IOException {
BufferedImage img = null;
File f = null;
File m = null;
int c = 0;
try {
f = new File("C:\\Users\\Lance Dean\\Desktop\\Java\\Sample.jpg");
img = ImageIO.read(f);
} catch (IOException e) {
System.out.println(e);
}
int width = img.getWidth();
int height = img.getHeight();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int p = img.getRGB(i, j);
int r = (p >> 16) & 0xff;
int g = (p >> 8) & 0xff;
int b = p & 0xff;
c++;
System.out.println(c);
int a = 4 * (int) (Math.floor(255 / 4));
int x = 4 * (int) (Math.floor((double) (r / 4)));
int y = 4 * (int) (Math.floor((double) (g / 4)));
int z = 4 * (int) (Math.floor((double) (b / 4)));
p = (a << 24) | (x << 16) | (y << 8) | z;
img.setRGB(i, j, p);
}
}
try {
m = new File("C:\\Users\\Lance Dean\\Desktop\\Java\\End.jpg");
if (!m.canRead()) {
m.setReadable(true);
}
ImageIO.write(img, "jpg", m);
} catch (IOException e) {
System.out.println(e);
}
}
I am trying to read an image, zoom it in to 80*60 and then zoom out the resulted image 5 times by bilinear Interpolation method. But I get this error : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4800 . Can anyone help me please?
this is what I have done :
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class BiInterpolationTest {
public static int zh;
public static int zw;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int[][] savedImage;
File f = new File ("F:\\Java\\Gray Scale Images\\3.jpg");
savedImage = readimage(f);
BufferedImage grayImage = new BufferedImage(savedImage.length, savedImage[0].length, BufferedImage.TYPE_BYTE_GRAY);
for (int i =0 ; i<savedImage.length ; i ++){
for (int j=0 ; j<savedImage[0].length ; j++){
int rgb = savedImage[i][j];
rgb = (rgb<<16)|(rgb<<8)|(rgb);
grayImage.setRGB(i, j, rgb);
BufferedImage zoomin =ScaledImage(grayImage, 80,60);
zh = zoomin.getHeight();
zw = zoomin.getWidth();
byte[] tempArr;
tempArr = extractBytes(zoomin);
// TODO Auto-generated catch block
byte [] zoomout;
zoomout = bilineraInterpolation(tempArr , 5);
InputStream in = new ByteArrayInputStream(zoomout);
BufferedImage bImageInterpolated;
try {
bImageInterpolated = ImageIO.read(in);
ImageIO.write(bImageInterpolated, "jpg", new File(
"F:/new-darksouls.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static BufferedImage ScaledImage(Image img, int w , int h){
BufferedImage resizedImage = new BufferedImage(w , h , BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2 = resizedImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0, 0, w, h, null);
g2.dispose();
return resizedImage;
}
/////////////////////////////////////
public static byte[] extractBytes (BufferedImage Image) throws IOException {
BufferedImage bufferedImage = new BufferedImage(Image.getHeight(), Image.getWidth(), BufferedImage.TYPE_BYTE_GRAY);
// get DataBufferBytes from Raster
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
return ( data.getData() );
}
public static int[][] readimage(File filename){
BufferedImage img;
try {
img = ImageIO.read(filename);
// Gray_scaled Image output
int width = img.getWidth();
int height = img.getHeight();
ImagePro.fw=width;
ImagePro.fh = height;
int [][] readimageVal = new int [width][height];
for (int i = 0; i<height ; i++){
for (int j =0 ; j<width ; j++){
Color c = new Color(img.getRGB(j, i));
int r= (int)(c.getRed() * 0.299)&0xff;
int g = (int)(c.getGreen() * 0.587)&0xff;
int b = (int)(c.getBlue() *0.114)&0xff;
int avg = ((r+b+g));
readimageVal[j][i] = avg;
}
}
return readimageVal;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static byte[] bilineraInterpolation(byte[] ImgData, int ratio) {
int wf = zw*ratio;
int hf = zh*ratio;
byte[] tempArr = new byte[wf*hf] ;
int A, B, C, D, x, y, index, g ;
float x_ratio = ((float)(zw))/wf ;
float y_ratio = ((float)(zh))/hf ;
float x_diff, y_diff ;
int in = 0 ;
for (int i=0;i<hf;i++) {
for (int j=0;j<wf;j++) {
x = (int)(x_ratio * j) ;
y = (int)(y_ratio * i) ;
x_diff = (x_ratio * j) - x ;
y_diff = (y_ratio * i) - y ;
index = y*zw+x ;
// range is 0 to 255 thus bitwise AND with 0xff
A = ImgData[index] & 0xff ;
B = ImgData[index+1] & 0xff ;
C = ImgData[index+zw] & 0xff ;
D = ImgData[index+zw+1] & 0xff ;
g = (int)(
A*(1-x_diff)*(1-y_diff) + B*(x_diff)*(1-y_diff) +
C*(y_diff)*(1-x_diff) + D*(x_diff*y_diff)
) ;
tempArr[in++] = (byte) g ;
}
}
return tempArr ;
}
}
I am trying to read an image, zoom it in to 80*60 and then zoom out the resulted image 5 times by bilinear Interpolation method. But I get this error : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4800 . Can anyone help me please?
this is what I have done :
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class BiInterpolationTest {
public static int zh;
public static int zw;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int[][] savedImage;
File f = new File ("F:\\Java\\Gray Scale Images\\3.jpg");
savedImage = readimage(f);
BufferedImage grayImage = new BufferedImage(savedImage.length, savedImage[0].length, BufferedImage.TYPE_BYTE_GRAY);
for (int i =0 ; i<savedImage.length ; i ++){
for (int j=0 ; j<savedImage[0].length ; j++){
int rgb = savedImage[i][j];
rgb = (rgb<<16)|(rgb<<8)|(rgb);
grayImage.setRGB(i, j, rgb);
BufferedImage zoomin =ScaledImage(grayImage, 80,60);
zh = zoomin.getHeight();
zw = zoomin.getWidth();
byte[] tempArr;
tempArr = extractBytes(zoomin);
// TODO Auto-generated catch block
byte [] zoomout;
zoomout = bilineraInterpolation(tempArr , 5);
InputStream in = new ByteArrayInputStream(zoomout);
BufferedImage bImageInterpolated;
try {
bImageInterpolated = ImageIO.read(in);
ImageIO.write(bImageInterpolated, "jpg", new File(
"F:/new-darksouls.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static BufferedImage ScaledImage(Image img, int w , int h){
BufferedImage resizedImage = new BufferedImage(w , h , BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2 = resizedImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0, 0, w, h, null);
g2.dispose();
return resizedImage;
}
/////////////////////////////////////
public static byte[] extractBytes (BufferedImage Image) throws IOException {
BufferedImage bufferedImage = new BufferedImage(Image.getHeight(), Image.getWidth(), BufferedImage.TYPE_BYTE_GRAY);
// get DataBufferBytes from Raster
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
return ( data.getData() );
}
public static int[][] readimage(File filename){
BufferedImage img;
try {
img = ImageIO.read(filename);
// Gray_scaled Image output
int width = img.getWidth();
int height = img.getHeight();
ImagePro.fw=width;
ImagePro.fh = height;
int [][] readimageVal = new int [width][height];
for (int i = 0; i<height ; i++){
for (int j =0 ; j<width ; j++){
Color c = new Color(img.getRGB(j, i));
int r= (int)(c.getRed() * 0.299)&0xff;
int g = (int)(c.getGreen() * 0.587)&0xff;
int b = (int)(c.getBlue() *0.114)&0xff;
int avg = ((r+b+g));
readimageVal[j][i] = avg;
}
}
return readimageVal;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static byte[] bilineraInterpolation(byte[] ImgData, int ratio) {
int wf = zw*ratio;
int hf = zh*ratio;
byte[] tempArr = new byte[wf*hf] ;
int A, B, C, D, x, y, index, g ;
float x_ratio = ((float)(zw))/wf ;
float y_ratio = ((float)(zh))/hf ;
float x_diff, y_diff ;
int in = 0 ;
for (int i=0;i<hf;i++) {
for (int j=0;j<wf;j++) {
x = (int)(x_ratio * j) ;
y = (int)(y_ratio * i) ;
x_diff = (x_ratio * j) - x ;
y_diff = (y_ratio * i) - y ;
index = y*zw+x ;
// range is 0 to 255 thus bitwise AND with 0xff
A = ImgData[index] & 0xff ;
B = ImgData[index+1] & 0xff ;
C = ImgData[index+zw] & 0xff ;
D = ImgData[index+zw+1] & 0xff ;
g = (int)(
A*(1-x_diff)*(1-y_diff) + B*(x_diff)*(1-y_diff) +
C*(y_diff)*(1-x_diff) + D*(x_diff*y_diff)
) ;
tempArr[in++] = (byte) g ;
}
}
return tempArr ;
}
}
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);
}
}
}
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){}
}
}