import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.lang.Exception.*;
import java.io.*;
import javax.imageio.*;
import javax.imageio.ImageIO;
import java.net.*;
import java.net.MalformedURLException;
class Blur1
{
public static void main ( String [] args)
{
float[] matrix = {0.111f, 0.111f, 0.111f,
0.111f, 0.111f, 0.111f,
0.111f, 0.111f, 0.111f,
};
BufferedImage sourceImage = null;
try
{
URL url = new URL("image_0054.jpg");
}
catch(MalformedURLException me)
{
System.err.println("Malformed URL Exception" + me.getMessage());
}
sourceImage = ImageIO.read(url);
BufferedImage destImage = null;
BufferedImageOp op = new ConvolveOp( new Kernel(3, 3, matrix) );
BufferedImage blurredImage = op.filter(sourceImage, destImage);
}
}

When i compile the above code, i am getting the following error

blur1.java:27: cannot find symbol
symbol  : variable url
location: class Blur1
sourceImage = ImageIO.read(url);

What might be the reason ?
Thanks in advance.

Variables declared in the 'try' block can't be seen outside of it.

Try this:

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.lang.Exception.*;
import java.io.*;
import javax.imageio.*;
import javax.imageio.ImageIO;
import java.net.*;
import java.net.MalformedURLException;

class Blur1 {
	public static void main(String[] args) throws Exception {
		float[] matrix = { 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, };
		BufferedImage sourceImage = null;
		URL url;
		try {
			url = new URL("image_0054.jpg");
		} catch (MalformedURLException me) {
			System.err.println("Malformed URL Exception" + me.getMessage());
			return;
		}
		sourceImage = ImageIO.read(url);
		BufferedImage destImage = null;
		BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, matrix));
		BufferedImage blurredImage = op.filter(sourceImage, destImage);
	}
}

Personally, I would let the exceptions kill the program, rather than just ignoring them. Like this:

import java.awt.image.*;
import javax.imageio.ImageIO;
import java.net.*;

class Blur1 {
	public static void main(String[] args) throws Exception {
		float[] matrix = { 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, };
		BufferedImage sourceImage = null;
		URL url = new URL("image_0054.jpg");
		sourceImage = ImageIO.read(url);
		BufferedImage destImage = null;
		BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, matrix));
		BufferedImage blurredImage = op.filter(sourceImage, destImage);
	}
}

Hi, I am using the following code.

import java.awt.image.*;
import javax.imageio.ImageIO;
import java.net.*;
import java.io.*;
class Blur2
{
public static void main(String[] args) throws Exception
{
float[] matrix = { 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, };
BufferedImage sourceImage = null;
File file = new File("flower.jpg");
sourceImage = ImageIO.read(file);
BufferedImage destImage = null;
BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, matrix));
BufferedImage blurredImage = op.filter(sourceImage, destImage);
}
}

I got the following exception

Exception in thread "main" java.awt.image.ImagingOpException: Unable to convolve src image
at java.awt.image.ConvolveOp.filter(ConvolveOp.java:180)
at Blur2.main(blur2.java:16)

I have tried the same code for various images and same exception is coming. Any where i need to tweak my code ?

Personally, I would let the exceptions kill the program, rather than just ignoring them. Like this:

import java.awt.image.*;
import javax.imageio.ImageIO;
import java.net.*;

class Blur1 {
	public static void main(String[] args) throws Exception {
		float[] matrix = { 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f, };
		BufferedImage sourceImage = null;
		URL url = new URL("image_0054.jpg");
		sourceImage = ImageIO.read(url);
		BufferedImage destImage = null;
		BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, matrix));
		BufferedImage blurredImage = op.filter(sourceImage, destImage);
	}
}

might seem logical, but this is without a doubt one of the worst things you could do.
a better way to do this, would be catching the exception, showing an error message and then closing the application.

'throws Exception' just makes sure that you throw your Exception to the calling method/class, but in case of the main method: there is none, so where are you throwing it to? you're just creating an ugly error in your prompt screen without really solving anything

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.