I'm using java to crop image when upload file, I set value and to try to crop but is not get correct size of image as I expected
This my code: (updated)
private BufferedImage cropImageSquare(byte[] image) throws IOException {
InputStream in = new ByteArrayInputStream(image);
BufferedImage originalImage = ImageIO.read(in);
System.out.println("Original Image Dimension: "+originalImage.getWidth()+"x"+originalImage.getHeight());
BufferedImage croppedImage = originalImage.getSubimage(300, 150, 500, 500);
System.out.println("Cropped Image Dimension: "+croppedImage.getWidth()+"x"+croppedImage.getHeight());
return croppedImage;
}
my photo:
I want to crop image as above image (red line) but my code is seem incorrect.
How to crop image as expect?
I want to crop image as above image (red line) but my code is seem incorrect.
So, your input image is 1024x811 and your "target" image is 928x690, which is roughly 0.906x0.8509 reduction/difference - so the real question is ... which one of those is the right value?
Through my testing, based on this image, 0.8509 produces the best result
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Test {
public static void main(String[] args) throws IOException {
BufferedImage crop = new Test().crop(0.8509);
System.out.println(crop.getWidth() + "x" + crop.getHeight());
ImageIO.write(crop, "jpg", new File("Square.jpg"));
}
public BufferedImage crop(double amount) throws IOException {
BufferedImage originalImage = ImageIO.read(Test.class.getResource("Cat.jpg"));
int height = originalImage.getHeight();
int width = originalImage.getWidth();
int targetWidth = (int)(width * amount);
int targetHeight = (int)(height * amount);
// Coordinates of the image's middle
int xc = (width - targetWidth) / 2;
int yc = (height - targetHeight) / 2;
// Crop
BufferedImage croppedImage = originalImage.getSubimage(
xc,
yc,
targetWidth, // widht
targetHeight // height
);
return croppedImage;
}
}
Now, this doesn't do any checks (xc + targetWidth > imageWidth), but I'm sure you can fill that out
Code is working fine for me.
public class Test {
public static void main( String[] args ) throws IllegalAccessException, InstantiationException {
try{
BufferedImage image = ImageIO.read(new File("C:\\Users\\guptab\\Pictures\\American.png"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] res=baos.toByteArray();
image = new Test().cropImageSquare(res);
}
catch(Exception e) {
e.printStackTrace();
System.out.println("Error");
}
}
private BufferedImage cropImageSquare(byte[] image) throws IOException {
InputStream in = new ByteArrayInputStream(image);
BufferedImage originalImage = ImageIO.read(in);
System.out.println("Original Image Dimension: "+originalImage.getWidth()+"x"+originalImage.getHeight());
BufferedImage croppedImage = originalImage.getSubimage(300, 150, 300, 600);
System.out.println("Cropped Image Dimension: "+croppedImage.getWidth()+"x"+croppedImage.getHeight());
return croppedImage;
}
}
Output is:
Original Image Dimension: 1279x1023
Cropped Image Dimension: 300x600
Definition of getSubImage method:
BufferedImage java.awt.image.BufferedImage.getSubimage(int x, int y, int w, int h)
Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.
Parameters:x the X coordinate of the upper-left corner of the specified rectangular regiony the Y coordinate of the upper-left corner of the specified rectangular regionw the width of the specified rectangular regionh the height of the specified rectangular regionReturns:a BufferedImage that is the subimage of this BufferedImage.
So int x and int y (First two Parameters are coordinates of image, not dimensions), only int w, int h (last two parameters) are dimensions of the image which is working fine.
Related
I have a difficulty cropping the image in Java. I have image that has black lines up and down. The image looks like this :
Image
I want to remove the black frame from the image and the width to remain the same. Please send me some solution to my problem. I tried something like this, but it crops only the bottom of the image.
BufferedImage originalImg = ImageIO.read(
new File(imageLocation));
// Fetching and printing alongside the
// dimensions of original image using getWidth()
// and getHeight() methods
System.out.println("Original Image Dimension: "
+ originalImg.getWidth()
+ "x"
+ originalImg.getHeight());
// Creating a subimage of given dimensions
BufferedImage SubImg
= originalImg.getSubimage(0,0,originalImg.getWidth(), 217);
// Printing Dimensions of new image created
System.out.println("Cropped Image Dimension: "
+ SubImg.getWidth() + "x"
+ SubImg.getHeight());
// Creating new file for cropped image by
// creating an object of File class
File outputfile
= new File(defaultPath+"crop_Image.jpg");
// // Writing image in new file created
ImageIO.write(SubImg, "jpg", outputfile);
// Display message on console representing
// // proper execution of program
System.out.println(
"Cropped Image created successfully");
}
You can use getRGB and setRGB to copy image area to the new image.
import java.awt.image.BufferedImage;
public class ImageUtil {
public static BufferedImage crop(BufferedImage image, int top, int right, int bottom, int left) {
int newWidth = image.getWidth() - left - right;
int newHeight = image.getHeight() - top - bottom;
int[] rgb = image.getRGB(left, top, newWidth, newHeight, null, 0, newWidth);
BufferedImage newImage = new BufferedImage(newHeight, newHeight, BufferedImage.TYPE_INT_ARGB);
newImage.setRGB(0, 0, newWidth, newHeight, rgb, 0, newWidth);
return newImage;
}
}
I have been trying to export my swing 2d diagram to a png file. I tried the following code :
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics2D referenceGraphics = bufferedImage.createGraphics();
referenceGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
paintComponent(referenceGraphics);
File imageFile = new File(fileName);
if (imageFile.exists() || imageFile.createNewFile()) {
ImageIO.write(bufferedImage, "png", imageFile);
}
However the png file is been created successfully, but the quality is too low. I need to create at least 300ppi image file. How to achieve that?
Simply increasing the size of the component won't make the resolution of the image any better. It will just be larger. What you need to do is create the BufferedImage larger that the component (i.e. 3 times for 3 times the current resolution) and the scale the Graphics object of the BufferedImage. The resulting code would look something like this:
public static void main(String[] args) throws IOException {
Component comp = ...
BufferedImage img = scaledImageFromComponent(comp, 3);
File imageFile = new File(fileName);
if (imageFile.exists() || imageFile.createNewFile()) {
ImageIO.write(img, "png", imageFile);
}
}
public static BufferedImage scaledImageFromComponent(final Component c, final double scale) {
c.setSize(c.getPreferredSize());
c.doLayout();
Rectangle r = new Rectangle(0, 0, c.getWidth(), c.getHeight());
return scaledImageFromComponent(c, r, scale, scale, false);
}
public static BufferedImage scaledImageFromComponent(final Component c, final Rectangle bounds,
final double scalex, final double scaley,
final boolean print) {
BufferedImage image = createCompatibleTransparentImage((int) (scalex * bounds.width),
(int) (scaley * bounds.height));
final Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d.scale(scalex, scaley);
g2d.translate(-bounds.x, -bounds.y);
if (print) {
c.print(g2d);
} else {
c.paint(g2d);
}
g2d.dispose();
return image;
}
public static BufferedImage createCompatibleTransparentImage(final int width,
final int height) {
return isHeadless() ? new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
: getGraphicsConfiguration().createCompatibleImage(width, height,
Transparency.BITMASK);
}
private static boolean isHeadless() {
return GraphicsEnvironment.isHeadless();
}
private static GraphicsConfiguration getGraphicsConfiguration() {
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}
If your diagram contains text you may want to switch Transparency.BITMASK with Transparency.OPAQUE for better anti-aliasing support (on Windows).
I have an image with transparent background. I'd like to rotate this image to a specific angle and keep the transparent background for the resulting image. For this purpose I use the following method:
public static BufferedImage rotateImage(BufferedImage image, double angle, Color backgroundColor) {
System.out.println(image.getType());
double theta = Math.toRadians(angle);
double sin = Math.abs(Math.sin(theta));
double cos = Math.abs(Math.cos(theta));
int w = image.getWidth();
int h = image.getHeight();
int newW = (int) Math.floor(w * cos + h * sin);
int newH = (int) Math.floor(h * cos + w * sin);
BufferedImage tmp = new BufferedImage(newW, newH, image.getType());
Graphics2D g2d = tmp.createGraphics();
if (backgroundColor != null) {
g2d.setColor(backgroundColor);
g2d.fillRect(0, 0, newW, newH);
}
g2d.fillRect(0, 0, newW, newH);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.translate((newW - w) / 2, (newH - h) / 2);
g2d.rotate(theta, w / 2, h / 2);
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
return tmp;
}
I invoke it with background=null:
BufferedImage image = ImageIO.read(file);
rotateImage(image, 4, null);
ImageIO.write(bi, "PNG", new File("image.png"));
but the background of the resulting image.png is WHITE. What am I doing wrong and how to properly keep the transparent background for image.png?
I'm a bit puzzled about the behavior of Graphics.drawImage(). Maybe somebody else can comment about it.
However, Graphics2D.drawRenderedImage() works a treat. It takes an AffineTransform to control the rotation. The below example nicely works. You probably have additional requirement about the final image size and the location of the rotated image.
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageRotation {
public static void main(String[] args) {
ImageRotation rotation = new ImageRotation();
rotation.rotate("input.png", 45, "output.png");
}
public void rotate(String inputImageFilename, double angle, String outputImageFilename) {
try {
BufferedImage inputImage = ImageIO.read(new File(inputImageFilename));
BufferedImage outputImage = rotateImage(inputImage, angle);
ImageIO.write(outputImage, "PNG", new File(outputImageFilename));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private BufferedImage rotateImage(BufferedImage sourceImage, double angle) {
int width = sourceImage.getWidth();
int height = sourceImage.getHeight();
BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = destImage.createGraphics();
AffineTransform transform = new AffineTransform();
transform.rotate(angle / 180 * Math.PI, width / 2 , height / 2);
g2d.drawRenderedImage(sourceImage, transform);
g2d.dispose();
return destImage;
}
}
Update
While the above code works for most PNGs, it does not work for the image that alexanoid is using. I've analyzed the image:
It's a grayscale image without a color palette (PNG color type 0) .
It uses simple transparency with a 2 byte long tRNS chunk.
As far as I can tell that's perfectly legal. However, ImageIO does not implement this combination. If the image has no palette, it simply ignores the tRNS chunk and therefore ignores the transparency information. That's most likely a bug.
You basically have two options now:
Look for an alternative library to read PNG files.
Fix the transparency after you have read the PNG file. This only works if know that the image used the particular problematic format.
Input and output for working PNG files
Input image:
Ouptput Image:
There is already question like this link on StackOverflow and the accepted answer is "casting":
Image image = ImageIO.read(new File(file));
BufferedImage buffered = (BufferedImage) image;
In my program I try:
final float FACTOR = 4f;
BufferedImage img = ImageIO.read(new File("graphic.png"));
int scaleX = (int) (img.getWidth() * FACTOR);
int scaleY = (int) (img.getHeight() * FACTOR);
Image image = img.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH);
BufferedImage buffered = (BufferedImage) image;
Unfortunatelly I get run time error:
sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
Obviously casting does not work.
Question is: What is (or is there) the proper way of converting Image to BufferedImage?
From a Java Game Engine:
/**
* Converts a given Image into a BufferedImage
*
* #param img The Image to be converted
* #return The converted BufferedImage
*/
public static BufferedImage toBufferedImage(Image img)
{
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
// Create a buffered image with transparency
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
// Return the buffered image
return bimage;
}
One way to handle this is to create a new BufferedImage, and tell it's graphics object to draw your scaled image into the new BufferedImage:
final float FACTOR = 4f;
BufferedImage img = ImageIO.read(new File("graphic.png"));
int scaleX = (int) (img.getWidth() * FACTOR);
int scaleY = (int) (img.getHeight() * FACTOR);
Image image = img.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH);
BufferedImage buffered = new BufferedImage(scaleX, scaleY, TYPE);
buffered.getGraphics().drawImage(image, 0, 0 , null);
That should do the trick without casting.
If you use Kotlin, you can add an extension method to Image in the same manner Sri Harsha Chilakapati suggests.
fun Image.toBufferedImage(): BufferedImage {
if (this is BufferedImage) {
return this
}
val bufferedImage = BufferedImage(this.getWidth(null), this.getHeight(null), BufferedImage.TYPE_INT_ARGB)
val graphics2D = bufferedImage.createGraphics()
graphics2D.drawImage(this, 0, 0, null)
graphics2D.dispose()
return bufferedImage
}
And use it like this:
myImage.toBufferedImage()
If you are getting back a sun.awt.image.ToolkitImage, you can cast the Image to that, and then use getBufferedImage() to get the BufferedImage.
So instead of your last line of code where you are casting you would just do:
BufferedImage buffered = ((ToolkitImage) image).getBufferedImage();
I am trying to resized a bufferedimage. I am able to store it and show up on a jframe no problems but I can't seem to resize it. Any tips on how I can change this to make it work and show the image as a 200*200 file would be great
private void profPic(){
String path = factory.getString("bottle");
BufferedImage img = ImageIO.read(new File(path));
}
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return dimg;
}
Updated answer
I cannot recall why my original answer worked but having tested it in a separate environment, I agree, the original accepted answer doesn't work (why I said it did I cannot remember either). This, on the other hand, did work:
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return dimg;
}
If all that is required is to resize a BufferedImage in the resize method, then the Thumbnailator library can do that fairly easily:
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
return Thumbnails.of(img).size(newW, newH).asBufferedImage();
}
The above code will resize the img to fit the dimensions of newW and newH while maintaining the aspect ratio of the original image.
If maintaining the aspect ratio is not required and resizing to exactly the given dimensions is required, then the forceSize method can be used in place of the size method:
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
return Thumbnails.of(img).forceSize(newW, newH).asBufferedImage();
}
Using the Image.getScaledInstance method will not guarantee that the aspect ratio of the original image will be maintained for the resized image, and furthermore, it is in general very slow.
Thumbnailator uses a technique to progressively resize the image which can be several times faster than Image.getScaledInstance while achieving an image quality which generally is comparable.
Disclaimer: I am the maintainer of this library.
Here's some code that I have used to resize bufferedimages, no frills, pretty quick:
public static BufferedImage scale(BufferedImage src, int w, int h)
{
BufferedImage img =
new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
int x, y;
int ww = src.getWidth();
int hh = src.getHeight();
int[] ys = new int[h];
for (y = 0; y < h; y++)
ys[y] = y * hh / h;
for (x = 0; x < w; x++) {
int newX = x * ww / w;
for (y = 0; y < h; y++) {
int col = src.getRGB(newX, ys[y]);
img.setRGB(x, y, col);
}
}
return img;
}
This class resize from a file and get the format name:
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import org.apache.commons.io.IOUtils;
public class ImageResizer {
public static void main(String as[]) throws IOException{
File f = new File("C:/Users/samsungrob/Desktop/shuttle.jpg");
byte[] ba = resize(f, 600, 600);
IOUtils.write(ba, new FileOutputStream( new File("C:/Users/samsungrob/Desktop/shuttle_resized.jpg") ) );
}
public static byte[] resize(File file,
int maxWidth, int maxHeight) throws IOException{
int scaledWidth = 0, scaledHeight = 0;
BufferedImage img = ImageIO.read((ImageInputStream) file );
scaledWidth = maxWidth;
scaledHeight = (int) (img.getHeight() * ( (double) scaledWidth / img.getWidth() ));
if (scaledHeight> maxHeight) {
scaledHeight = maxHeight;
scaledWidth= (int) (img.getWidth() * ( (double) scaledHeight/ img.getHeight() ));
if (scaledWidth > maxWidth) {
scaledWidth = maxWidth;
scaledHeight = maxHeight;
}
}
Image resized = img.getScaledInstance( scaledWidth, scaledHeight, Image.SCALE_SMOOTH);
BufferedImage buffered = new BufferedImage(scaledWidth, scaledHeight, Image.SCALE_REPLICATE);
buffered.getGraphics().drawImage(resized, 0, 0 , null);
String formatName = getFormatName( file ) ;
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(buffered,
formatName,
out);
return out.toByteArray();
}
private static String getFormatName(ImageInputStream iis) {
try {
// Find all image readers that recognize the image format
Iterator iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
// No readers found
return null;
}
// Use the first reader
ImageReader reader = (ImageReader)iter.next();
// Close stream
iis.close();
// Return the format name
return reader.getFormatName();
} catch (IOException e) {
}
return null;
}
private static String getFormatName(File file) throws IOException {
return getFormatName( ImageIO.createImageInputStream(file) );
}
private static String getFormatName(InputStream is) throws IOException {
return getFormatName( ImageIO.createImageInputStream(is) );
}
}
This is a shortened version of what is actually happening in imgscalr, if you just want to use the "balanced" smoothing:
/**
* Takes a BufferedImage and resizes it according to the provided targetSize
*
* #param src the source BufferedImage
* #param targetSize maximum height (if portrait) or width (if landscape)
* #return a resized version of the provided BufferedImage
*/
private BufferedImage resize(BufferedImage src, int targetSize) {
if (targetSize <= 0) {
return src; //this can't be resized
}
int targetWidth = targetSize;
int targetHeight = targetSize;
float ratio = ((float) src.getHeight() / (float) src.getWidth());
if (ratio <= 1) { //square or landscape-oriented image
targetHeight = (int) Math.ceil((float) targetWidth * ratio);
} else { //portrait image
targetWidth = Math.round((float) targetHeight / ratio);
}
BufferedImage bi = new BufferedImage(targetWidth, targetHeight, src.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); //produces a balanced resizing (fast and decent quality)
g2d.drawImage(src, 0, 0, targetWidth, targetHeight, null);
g2d.dispose();
return bi;
}
try the imgscalr library. Best lib i found- very fast, good quality and simple to use
BufferedImage thumbnail = Scalr.resize(image, 150);
deprecated link: http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/
Apache 2 License
Check this out, it helps:
BufferedImage bImage = ImageIO.read(new File(C:\image.jpg);
BufferedImage thumbnail = Scalr.resize(bImage, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_WIDTH,
750, 150, Scalr.OP_ANTIALIAS);