0

我是 Processing 的新手,并且一直在玩弄它的功能,但我一生都无法弄清楚如何正确调整图像大小。下面是教程中的一个函数示例,但我似乎无法正确调整图像大小以使其适合窗口,有人有什么建议吗?

导入处理.core.*;

public class Adjusting_image_brightness extends PApplet
{
    //intiallize image
    PImage img;
    public void setup()
    {
        //set the size
        size(320,240);
        //load the image
        img = loadImage("Conn4Board2.jpg");     
        //img.resize(400, 400);

    }
    public void draw()
    {
        //call the pixels
        loadPixels();
        //run through the pixels
        for(int x = 0; x < img.width; x++)
        {
            for(int y = 0; y <img.height ; y++)
            {
                //calculate the 1D pixel location 
                int loc = x + y*width;
                //get the R G B values from the picture
                float r = red(img.pixels[loc]);
                float b= blue(img.pixels[loc]);
                float g = green(img.pixels[loc]);

                //change the brightness acording to the mouse here
                double  adjustBrightness = ((float) mouseX / width) * 8.0;
                r *= adjustBrightness;
                b *= adjustBrightness;
                g *= adjustBrightness;


                    //Constrainr RGB to between 0 - 255
                    r = constrain(r, 0 , 255);
                    g = constrain(g, 0 , 255);
                    b = constrain(b, 0, 255);

                    //make a new colour and set pixel in the window 
                    int c = color(r,g,b);
                    pixels[loc] = c;

            }
        }
         updatePixels();
    }
}

谢谢斯蒂芬。

4

1 回答 1

0

如果您只想调整图像大小以使其适合窗口,则代码过多。image()函数已经让你说它应该适合哪个边界框,所以只需使用这个:

image(img, 0, 0, width, height);

并且您的图像将按比例绘制。

于 2013-02-04T23:25:03.913 回答