cropping image in java

cropping image dynamically and saving cropped image in our desired for

Page 1 of 1

4 Replies - 23726 Views - Last Post: 22 September 2010 - 09:38 AM Rate Topic: -----

#1 rajvalyal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-December 09

cropping image in java

Post icon  Posted 16 December 2009 - 09:29 PM

Thanks for ur solution.
by executing the code below am able to save image.But i want to crop an image then save it and convert it to desired image format.


import java.awt.*;
import javax.swing.*;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
import java.lang.*;

public class CropImage extends JFrame {
Image image;
Insets insets;
BufferedImage img = null;


public CropImage() 
{
super("CropImage");
//ImageIcon icon = new ImageIcon("photo.jpg");
//image = icon.getImage();
//image = createImage(new FilteredImageSource(image.
//getSource(),new CropImageFilter(100, 70, 140, 250)));


try {
	img = ImageIO.read(new File("photo.jpg"));
	
	File outputfile = new File("saved.gif");
	ImageIO.write(img, "gif", outputfile);

	}
 	catch (IOException e) {
	
	}

	}
   public void paint(Graphics g)
   {
  super.paint(g);
  if (insets == null) 
 {
 insets = getInsets();
 }
 g.drawImage(img, insets.left, insets.top, this);
 }
 public static void main(String args[]) {
 JFrame frame = new CropImage();
 frame.setSize(250, 250);
 frame.show();
 }
 }



Is This A Good Question/Topic? 0
  • +

Replies To: cropping image in java

#2 Guest_kalpana*


Reputation:

Re: cropping image in java

Posted 22 September 2010 - 01:12 AM

hi..
i want codings for cropping an image i circle shape
Was This Post Helpful? 0

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3464
  • View blog
  • Posts: 15,676
  • Joined: 20-September 08

Re: cropping image in java

Posted 22 September 2010 - 04:20 AM

Easier to use BufferedImage.getSubimage:

import java.awt.*;
import java.awt.image.*;

import java.net.URL;

import java.io.*;

import java.lang.*;

import javax.imageio.*;

import javax.swing.*;


public class CropImage extends JFrame {
    Image image;
    Insets insets;
    BufferedImage img = null;

    public CropImage() {
        super("CropImage");
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
	    String dicUrl = "http://a1.phobos.apple.com/us/r30/Podcasts/05/64/55/ps.bxtoijun.170x170-75.jpg";
            img = ImageIO.read(new URL(dicUrl));


            File outputfile = new File("saved.gif");
	    // Get the 'D' logo from the DIC Podcast image
            ImageIO.write(img.getSubimage(44, 21, 78, 78), "gif", outputfile);
        } catch (IOException e) {
        }
    }

    public void paint(Graphics g) {
        super.paint(g);

        if (insets == null) {
            insets = getInsets();
        }

        g.drawImage(img, insets.left, insets.top, this);
    }

    public static void main(String[] args) {
        JFrame frame = new CropImage();
        frame.setSize(250, 250);
        frame.show();
    }
}



Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12179
  • View blog
  • Posts: 45,247
  • Joined: 27-December 08

Re: cropping image in java

Posted 22 September 2010 - 07:08 AM

@g00se: Reeks of homework 100 miles around, plus it's a necropost. :)
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3464
  • View blog
  • Posts: 15,676
  • Joined: 20-September 08

Re: cropping image in java

Posted 22 September 2010 - 09:38 AM

Sorry - i'll keep my eye on dates in future
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1