Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36438

Re: Can you resize with BufferedImage

From Martin Gregorie <martin@address-in-sig.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Can you resize with BufferedImage
Date 2017-04-12 13:06 +0000
Organization A noiseless patient Spider
Message-ID <ocl8os$qc7$1@dont-email.me> (permalink)
References <871ssy10ub.fsf@Equus.decebal.nl> <ockuu9$oh6$1@dont-email.me> <87wpapzv42.fsf@Equus.decebal.nl>

Show all headers | View raw


On Wed, 12 Apr 2017 14:08:29 +0200, Cecil Westerhof wrote:

> 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.

That sounds right - first select what you want to have in the image and 
then scale it to whatever size you want. 

I don't remember where I would have found the example code that became 
what I published, but I do remember going to look for it once I realised 
that the Java tutorials, package and class-level comments was absolutely 
bloody useless when it comes to explaining how to choose the classes 
you'll need to carry out a task or the best order to apply them in. They 
make the unstated assumption that any programmer who needs to use the 
classes in java.awt.image and java.awt.image.renderable will already 
understand everything they need to know about the purpose of the classes 
they contain and which ones to use for any given task.

I, for one, don't have an image processing background apart from being a 
self-taught GIMP user and, on the face of it, nor does Cecil, so if 
anybody reading this can recommend a book or a website that covers the 
basics of programming with these tools, please post the link here.

Speaking entirely for myself, something with the approach and content of 
the better O'Reilly books would be perfect.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Can you resize with BufferedImage Cecil Westerhof <Cecil@decebal.nl> - 2017-04-12 10:34 +0200
  Re: Can you resize with BufferedImage Martin Gregorie <martin@address-in-sig.invalid> - 2017-04-12 10:18 +0000
    Re: Can you resize with BufferedImage Cecil Westerhof <Cecil@decebal.nl> - 2017-04-12 14:08 +0200
      Re: Can you resize with BufferedImage Martin Gregorie <martin@address-in-sig.invalid> - 2017-04-12 13:06 +0000
        Re: Can you resize with BufferedImage Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-04-12 10:05 -0500
          Re: Can you resize with BufferedImage Eric Douglas <e.d.programmer@gmail.com> - 2017-04-12 08:20 -0700
            Re: Can you resize with BufferedImage Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-04-13 11:10 -0500
          Re: Can you resize with BufferedImage Martin Gregorie <martin@address-in-sig.invalid> - 2017-04-12 16:13 +0000

csiph-web