Blurring an image got from JFilechooser

Joined
Mar 21, 2010
Messages
4
Reaction score
0
Hi people,

I was wondering if i could get some help with a problem I've run into? I have created a project in Netbeans which gets a JPEG file from my directory using JFilechooser. The image is displayed on a JLabel as an ImageIcon...

Code:
private void import_standby_ButActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        fc.setCurrentDirectory(new File("user.dir"));
        fc.setAcceptAllFileFilterUsed (false);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG Images", "jpg");
        fc.setFileFilter(filter);
        fc.setAccessory(new ImagePreview(fc));
        int result = fc.showOpenDialog(null);

        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fc.getSelectedFile();
            System.out.println(selectedFile.getParent());
            File file = fc.getSelectedFile();
            System.out.println(selectedFile.getName());
            // Read image and place new file icon into preferred locations
            BufferedImage newImage = null;
            try {
                newImage = ImageIO.read(file);
            } catch(IOException e) {
                System.out.println("read error: " + e.getMessage());
            }

            ImageIcon backgdIcon = new ImageIcon(newImage);
            stadbyImgLabel.setIcon(backgdIcon);

            // Enables Remove Button
            standby_remove_But.setEnabled(true);

        } else if (result == JFileChooser.CANCEL_OPTION) {
            System.out.println(JFileChooser.CANCEL_OPTION);
        }
}

Now i want to blur the image and ive tried modifying the following code...

Code:
Image displayImage;

  BufferedImage biSrc;

  BufferedImage biDest; // Destination image is mandetory.

  BufferedImage bi; // Only an additional reference.

  Graphics2D big;

  CPanel() {
    setBackground(Color.black);
    loadImage();
    setSize(displayImage.getWidth(this), displayImage.getWidth(this));
    createBufferedImages();
    bi = biSrc;
  }

  public void loadImage() {
    displayImage = Toolkit.getDefaultToolkit().getImage("displayImage.jpg");// is this correct???
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(displayImage, 1);
    try {
      mt.waitForAll();
    } catch (Exception e) {
      System.out.println("Exception while loading.");
    }
    if (displayImage.getWidth(this) == -1) {
      System.out.println("No jpg file");
      System.exit(0);
    }
  }

  public void createBufferedImages() {
    biSrc = new BufferedImage(displayImage.getWidth(this), displayImage
        .getHeight(this), BufferedImage.TYPE_INT_RGB);

    big = biSrc.createGraphics();
    big.drawImage(displayImage, 0, 0, this);

    biDest = new BufferedImage(displayImage.getWidth(this), displayImage
        .getHeight(this), BufferedImage.TYPE_INT_RGB);
  }

  public void blur() {
    float data[] = { 0.0625f, 0.125f, 0.0625f, 0.125f, 0.25f, 0.125f,
        0.0625f, 0.125f, 0.0625f };
    Kernel kernel = new Kernel(3, 3, data);
    ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
        null);
    convolve.filter(biSrc, biDest);
    bi = biDest;
  }

but i cant get anywhere. Can someone please point me in the right direction as to how/where I load the image?
 
Joined
Mar 21, 2010
Messages
4
Reaction score
0
Is it something long the lines of this??

Code:
public void setCurrentPicture(final File imageFile) throws Exception {
 
			currentImage = (Image) Toolkit.getDefaultToolkit().createImage(
					imageFile.getPath()) ;
 
			/* Essentially an image observer that can track when images are
			 * ready for rendering to the specified component. */
			MediaTracker md = new MediaTracker(this) ;
 
			md.addImage(currentImage, 0) ;
 
			/* Blocks until images are ready for drawing on panel or some
			 * error occured. */
			md.waitForAll() ;
 
			/* If any errors are reported then the loading of the image has
			 * failed so some custom exception is thrown. */
			if (md.isErrorAny()) throw new Exception("Error while " +
					"loading image.") ;
 
			//bCurrentImage.getGraphics().dispose() ;
			bCurrentImage.getGraphics().clearRect(0,0,500,500) ;
 
			/* Create a buffered image from loaded image. */
			 bCurrentImage.getGraphics().drawImage(currentImage,x*100,y*100,100,100,this) ;
 
			/* Repaint canvas with the new image current image. */
			repaint() ;
		}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top