我最近制作了一个处理Java的程序来加载图像并模糊它但是现在把这个函数作为一个方法放到一个类中并且出现了这个错误:
无法在原始类型int
上调用blurImage(int,int,int)
我的班级:
//create class called ImageProcessing where all image editing methonds will be
class ImageProcessing {
//create function "blurImage" which takes in an xOffset, a yOffset, and a blur amount
//xOffset and yOffset is changing where the grid starts so the make the blurring algorithm less pixelated using other methods when calling this function
//the blur amount is how big each box in the grid is
void blurImage(int xOffset, int yOffset, int blurAmount) {
//loop through x in the image starting at the negative xOffset and incrementing by the blurAmouint / 2
//the negative xOffset's purpose is to start the offset off the grid so as to blur the whole image no matter the offset
//incrementing by the blurAmount / 2 is done because it needs to go in both directions
for (int x = -xOffset; x < pic.width; x += blurAmount / 2) {
//loop through the same way for the y so that we can got though in 2 dimentions instead of just 1
for (int y = -yOffset; y < pic.height; y += blurAmount / 2) {
//create a 2D array called p that is the size of the blur amount, the size of the box; this is the box
color[][] p = new color[blurAmount][blurAmount];
//initialize variables that are the averages of each color channel
int redAverage = 0;
int greenAverage = 0;
int blueAverage = 0;
//loop through the blurAmount in both dimentions to refrence all the pixels in the p array
for (int a = 0; a < blurAmount; a++) {
for (int b = 0; b < blurAmount; b++) {
//fill the array with the colors of the current box on the grid so i can refrence them easily
p[a][b] = pic.get(x + a, y + b);
//these lines add up the Red Green and Blue channels into their individual variables
//the "(p[a][b] >> 16) & 0xFF" is first shifting the hexidecimal number over into the correct position and then chopping off the extra bits to the right
redAverage += (p[a][b] >> 16) & 0xFF;
greenAverage += (p[a][b] >> 8) & 0xFF;
blueAverage += p[a][b] & 0xFF;
}
}
//dividing the red averages by the square of the blurAmount
//the squaring is needed to find the area in pixels, this makes the average
redAverage /= (int)(blurAmount * blurAmount);
greenAverage /= (int)(blurAmount * blurAmount);
blueAverage /= (int)(blurAmount * blurAmount);
//create color from the average values
color blur = color(redAverage, greenAverage, blueAverage);
//loop through the current box on the grid the same way as before
for (int a = 0; a < blurAmount; a++) {
for (int b = 0; b < blurAmount; b++) {
//setting all the pixels in the box to the color of the averages
//the reason for the "x + a" and the "y + b":
//find the location of the pixels in the box relative to the top left corner of it
pic.set(x + a, y + b, blur);
}
}
}
}
}
}
我的代码叫它:
/*
//The purpose of this program is to create a blurring algorithm that can
//adjust to pixelate or blur the image at different levels.
//
//We first need to split th image into a grid and work with each box on the grid individually.
//After this we get all the color values from each pixel in the box and store it in our code.
//We can then add all the reds together, the greens together, and the blues together and divide each by the number of pixels.
//Next, we turn all the three average into a color variable and set all the pixels in the current box the that color
//
//If we run this multiple times, we can change the blur amount each time and offset the grid to get less pixelated results, we can get a smoother image.
//
//Kwown Issues:
//none so far
//
//Created by Matthew Teta
*/
//create instance of my ImageProcessing claa
ImageProcessing i = new ImageProcessing();
//Make sketch full screen
boolean sketchFullScreen() {
return true;
}
//initialize image
PImage pic;
void setup() {
//load image from file and save it into pic PImage
pic = loadImage("image.jpg");
//create window at size of picture
size(pic.width, pic.height);
background(255);
frameRate(1);
}
//maxBlur is the starting variable for the blur; TL;DR sets the first size of the grid boxes; also used for the temperary blur value running later in my code
float maxBlur = 16;
//initialize the blurDecrement
int blurDecrement = 0;
//create variable that is the amount of times the blurring algorithm is run
//RECOMENDATION: make the blur half of the maxBlur
//the lower the value the more pixelated
int blur = 1;
//simple boolean so that my code for running the blur is only run once at the beginning
boolean first = true;
void draw() {
//calling "first()" if this is the first iteration
if (first) {
first();
}
//save image in file called "blurred-img" as a jpg
pic.save("blurred-img.jpg");
//setting the variable for runniong the code once to false so that is won't be run again
first = false;
}
//create function first that is only run one time when the program is run
void first() {
//first find the blurDecrement so that we will get a smooth transition for the number of iterations of the blur algorithm we have
blurDecrement = (int)(maxBlur / blur);
//iterate through blur, the number of times to blur
for (int i = 0; i < blur; i++) {
//run blur algorithm at the blurDecrement for the x and y offsets using the maxBlur
//remember: the maxBlur is also used to store the value after being decremented in the loop
i.blurImage(blurDecrement, blurDecrement, round(maxBlur));
//draw the image on the canvas at the top left point, (0, 0)
image(pic, 0, 0);
//decrement maxBlur by the blurDecrement value
maxBlur -= blurDecrement;
}
}
错误在行
i.blurImage(blurDecrement,blurDecrement,round(maxBlur));
我在互联网上搜索了这个问题,但还没有得到我想要的东西。提前感谢您的回复!
答案 0 :(得分:1)
提示,您将i
声明为int i = 0
中的int。请记住,局部变量优先于全局变量。尝试将对象i
重命名为object_i
。即ImageProcessing object_i = new ImageProcessing();
for (int i = 0; i < blur; i++) {
i.Func(foo);// the problem = object name is same as int variable name.
//stuff
}