CropField selected area

Hello there, I’ve faced with a couple of problems when using CropField addon for vaadin and I can’t find any appropriate example or documentation to resolve this problem.
The purpose of CropField is to get only selected part of the image. Which method does that? getImageResource() returns the whole image.

The second question is - is it possible to set a cropfield with a predetermined selected area? I’ve set minimal and maximal values of width and height to the same number so that selection area is always the same. However, user have to click on the cropfield in order to see it.

Thanks.

Hi there,

sorry for answering your question that late :wink:

There is no function for cropping the image to the correct size implemented. As there can be different image manipulation APIs (java, PDF images etc.) and use cases for our component we did not implement it directly into the component. you have to handle it yourself in your application. I can give you the code we were using for cropping to the final image using java imaging API:


	private BufferedImage performImageCrop(BufferedImage image,
			VCropSelection selection, int destinationWidth,
			int destinationHeight) {
		BufferedImage dest = new BufferedImage(destinationWidth,
				destinationHeight, image.getType());
		Graphics2D g = dest.createGraphics();

		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.setRenderingHint(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		g.drawImage(image, 0, 0, destinationWidth, destinationHeight,
				selection.getXTopLeft(), selection.getYTopLeft(),
				selection.getXBottomRight(), selection.getYBottomRight(), null);

		g.dispose();
		return dest;
	}

You should be able to set the CropFields selection using the setValue() function of the component. just create an instance of VCropSelection and fill it for your needs.
i have not testet it yet, but the code looks fine and that’s the way Fields work.

Best wishes from Berlin,

Stefan Meißner
davengo GmbH

Cropfield does not have any method that returns a BufferedImage, could you pelae tell me where will I get this object from… Iam new to Java Imaging and facing difficulty using this add-on

There are various ways to get a BufferedImage of your Image…

ImageIO.read(File input);
ImageIO.read(ImageInputStream stream);
ImageIO.read(InputStream input);
ImageIO.read(URL input);


http://docs.oracle.com/javase/1.4.2/docs/api/javax/imageio/ImageIO.html