A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/coobird/thumbnailator/issues/207 below:

Incorrect cropping when specifying sourceRegion and an image that has exif orientation · Issue #207 · coobird/thumbnailator · GitHub

Expected behavior

Using a source region with an image that has exif orientation should effectively crop the region where specified after the orientation has been applied.

Actual behavior

The output image has the wrong area of the source image.

Steps to reproduce the behavior

Using a source image that has exif orientation (in this case it was top right orientated) use the following code to read the image from inputStream and write to outputStream:

Thumbnails
	.of(inputStream)	
	.useExifOrientation(true)									
	.size(256, 256)		
	.sourceRegion(cropX, cropY, cropWidth, cropHeight)			
	.outputFormat("JPEG")
	.outputQuality(1)
	.toOutputStream(outputStream);

This will produce an incorrect result.

Looking at the code during the readImage method of InputStreamImageSource it adds a filter to work with the orientation. However, it then calculates the source region a bit further down before any filters are run and is using the original width and height. I'm assuming this maybe why it doesn't correctly produce the cropped image.

Workaround

My current workaround for this is to first create a BufferedImage to correct the orientation, and then create the cropped version from this BufferedImage as follows:

BufferedImage img = Thumbnails
	.of(inputStream)	
	.scale(1)
	.useExifOrientation(true)
	.outputFormat("JPEG")
	.outputQuality(1)
	.asBufferedImage();
				
Thumbnails
	.of(img)										
	.size(256, 256)
	.sourceRegion(cropX, cropY, cropWidth, cropHeight)			
	.outputFormat("JPEG")
	.outputQuality(1)
	.toOutputStream(outputStream);
Environment

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4