处理图像加载错误

时间:2018-02-17 12:22:07

标签: processing

我已经在我的处理文件中添加了一个背景图片,当我在本地处理时运行它时工作正常,但是,当我托管文件时,我在控制台中收到此错误:

Uncaught Error using image in background(): PImage not loaded.

我无法找到可能发生这种情况的地方。请参阅下面的代码:

Particle p = new Particle();

final int LIGHT_FORCE_RATIO = 70;   final int  LIGHT_DISTANCE= 70 * 50;   final int  BORDER = 100;   float baseRed, baseGreen, baseBlue;   float baseRedAdd, baseGreenAdd, baseBlueAdd;   final float RED_ADD =
3.2;    final float GREEN_ADD = 1.7;   final float BLUE_ADD = 4.3;   float boxX, boxY; float widthSize = width;

int rectWidth = 915; int rectHeight = 197;

PImage img;

void setup() { img = loadImage("https://s3.amazonaws.com/zcom-media/sites/a0iE000000QK9yZIAT/media/mediamanager/bannerbg.jpg");

  background(img);   size(1840, 400);  // surface.setResizable(true);  noCursor();   //img = loadImage("flowtoy.jpg");   baseRed = 0;   baseRedAdd = RED_ADD;

  baseGreen = 0;   baseGreenAdd = GREEN_ADD;

  baseBlue = 0;   baseBlueAdd = BLUE_ADD;

  boxX = width/2;   boxY = height/2;
     img.loadPixels(); for (int i = 0; i < img.pixels.length; i++) {   img.pixels[i] = color(0, 90, 102);  } img.updatePixels(); }

void draw() {   //image(img, 400, 100, img.width/2, img.height/2);   if (mouseX>boxX-rectWidth && mouseX<boxX+rectWidth &&
    mouseY>boxY-rectHeight && mouseY<boxY+rectHeight) {

    //saveFrame("output/LightDrawing_####.png");

    baseRed += baseRedAdd;
    baseGreen += baseGreenAdd;
    baseBlue += baseBlueAdd;

    colorOutCheck();

    p.move(mouseX, mouseY);


    int tRed = (int)baseRed;
    int tGreen = (int)baseGreen;
    int tBlue = (int)baseBlue;

    tRed *= tRed;
    tGreen *= tGreen;
    tBlue *= tBlue;


    loadPixels();

    int left = max(0, p.x - BORDER);
    int right = min(width, p.x + BORDER);
    int top = max(0, p.y - BORDER);
    int bottom = min(height, p.y + BORDER);


    for (int y = top; y < bottom; y++) {
      for (int x = left; x < right; x++) {
        int pixelIndex = x + y * width;

        int r = pixels[pixelIndex] >>16 & 0xFF;
        int g = pixels[pixelIndex] >> 8 & 0xFF;
        int b = pixels[pixelIndex] & 0xFF;


        int dx = x - p.x;
        int dy = y - p.y;
        int distance = (dx *dx ) + (dy* dy);  


        if (distance < LIGHT_DISTANCE) {
          int fixFistance = distance * LIGHT_FORCE_RATIO;

          if (fixFistance == 0) {
            fixFistance = 1;
          }   
          r = r + tRed / fixFistance;
          g = g + tGreen / fixFistance;
          b = b + tBlue / fixFistance;
        }
        pixels[x + y * width] = color(r, g, b);
      }
    }

     updatePixels();   } else { 
    setup();   }

  //updatePixels(); }


void colorOutCheck() {   if (baseRed < 10) {
    baseRed = 10;
    baseRedAdd *= -1;   } else if (baseRed > 255) {
    baseRed = 255;
    baseRedAdd *= -1;   }

  if (baseGreen < 10) {
    baseGreen = 10;
    baseGreenAdd *= -1;   } else if (baseGreen > 255) {
    baseGreen = 255;
    baseGreenAdd *= -1;   }

  if (baseBlue < 10) {
    baseBlue = 10;
    baseBlueAdd *= -1;   } else if (baseBlue > 255) {
    baseBlue = 255;
    baseBlueAdd *= -1;   } }

void mousePressed() {   setup(); }

class Particle {   int x, y;           float vx, vy;       //float slowLevel;

    Particle() {
    x = (int)3;
    y = (int)2;  //   slowLevel = random(100) + 1;   }
    void move(float targetX, float targetY) {
    vx = (targetX - x) ;
    vy = (targetY - y) ;

    x = (int)(x + vx);
    y = (int)(y + vy);   } }

有人可以解释为什么会这样吗?非常感谢,谢谢。

1 个答案:

答案 0 :(得分:0)

我假设通过托管文件&#39;你的意思是在www.openprocessing.org上运行草图。

您的代码存在两个问题:

  1. 在设置中使用background()。这不起作用,您将收到您提到的错误。您可以尝试将其移动到draw()。
  2. 使用托管在其他服务器中的图像。你应该托管你自己的图像。
  3. 问题2的解决方法是将图像存储为base64编码的字符串。

    String uriBase64 = "data:image/jpeg;base64,/9j/4AAQSkZJRgA";
    img = loadImage(uriBase64);
    

    此处的完整示例:https://www.openprocessing.org/sketch/511424