Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: Can you resize with BufferedImage Date: Wed, 12 Apr 2017 14:08:29 +0200 Organization: Decebal Computing Lines: 44 Message-ID: <87wpapzv42.fsf@Equus.decebal.nl> References: <871ssy10ub.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="8c920cbe18dfef48002d853848ae82dc"; logging-data="21896"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18sLDrdVMIxaoRcpJfyYLld7q6EwDiV1KE=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:gzODz3SoclqQbD1waT3gTN/Q4xc= sha1:0HpU6VOyM6R96/zvQ7viV87W2gc= Xref: csiph.com comp.lang.java.programmer:36434 On Wednesday 12 Apr 2017 12:18 CEST, Martin Gregorie wrote: > On Wed, 12 Apr 2017 10:34:04 +0200, Cecil Westerhof wrote: > >> In my code I have the following code: >> dest = image.getSubimage(x, y, dispWidth, dispHeight); >> >> This is the part that I want to select, but I want to resize it at >> the same time. In this case I would like to scale to 50%. How >> should I do that? > > Use an AffineTransform. > > The image resizing code should look something like this fragment: > > AffineTransform at = new AffineTransform(); > at.scale(scale, scale); > AffineTransformOp op = new AffineTransformOp(at, > AffineTransformOp.TYPE_BICUBIC); > Double nw = new Double(w * scale); > Double nh = new Double(h * scale); > om = new BufferedImage(nw.intValue(), > nh.intValue(), > BufferedImage.TYPE_3BYTE_BGR); > op.filter(im, om); > > where: > im is a BufferedImage containing the old image > om is a BufferedImage to hold the modified image > scale is a double containing the required scaling factor > > I'm scaling images to a width of 400 pixels, so I calculate scale as > 400.0 / image_width. You would just set scale to 0.5. I want to keep > the image proportion, so I use the same scale value for both width > and height. Does not look to difficult. Just to be sure I understand it correctly. I first cut the part I want and then I do the scale. So my dest becomes im. And then I use your code. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof