简体   繁体   中英

Java image zoom

I need help in my code.

int width = img.getXDim();
int height = img.getYDim();
int n = 3;

Image newImg = new ByteImage(width * n, height * n, 1);

        for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){

    int p = img.getXYByte(x, y);

    newImg.setXYByte(n * x, n * y, p);
    newImg.setXYByte(n * x + 2, n * y, p);
    newImg.setXYByte(n * x, n * y + 2, p);
    newImg.setXYByte(n * x + 2, n * y + 2, p) ;  `

My problem is, I want to change n value (as 2, 9 or 0.5) so I can zoom in or zoom out my image. But when I write float n for decimal numbers, setXYByte says that I can use only int,int,int values. With this code, I can only make my image 3 or more times bigger. Sorry for my bad English.

You can "cast" floating point values to ints like this:

newImg.setXYByte((int) (n * x), (int) (n * y), p);

Casting discards the fractional part of the number so that something like 21.7 becomes 21.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM