Google image loader library

edited April 2018 in Library Questions

Hi there, I'm using a googleImageLoader library. I got my API key and it worked well. But I want to make user search images they want( The original code was set to find "sunset beach" ). so I changed some codes but they are really new for me that I can't find what is wrong. Is there anyone who can help me, please?

import at.mukprojects.imageloader.*;
import at.mukprojects.imageloader.google.*;
import at.mukprojects.imageloader.image.*;

String apiKey = "---------------  ";

ImageLoader loader;
ImageList list;
Image img;

String search = " ";

void setup() {
  size(800, 450);
  loader = new GoogleLoader(this, apiKey);
}

void draw() {

  textSize(64);
  fill(0);
  text(search, width/2, height/2); //show typing which image the user want to find

  if (mousePressed) {
    list = loader.start(search, true, 60 * 1000);
    img = list.getRandom();
    if (img == null) {
      img = list.getRandom();
    } else {
      image(img.getImg(), 0, 0, width, height);
    }
  }
}

void keyPressed() {
  search += key;
}

Answers

  • Are you following this link? https://github.com/keshrath/ImageLoader

    Can you comment how did you get your api key?

    Quick side note:

    Remove the code block of mousePressed inside draw(). Instead do this:

    void mousePressed() {
        list = loader.start(search, true, 60 * 1000);
        img = list.getRandom();
        if (img == null) {
          img = list.getRandom();
        } else {
          image(img.getImg(), 0, 0, width, height);
        }
    }
    

    Your code could potentially execute as many times as mousePressed is true. My suggestion should work only once. This will not solve the problem but it is more of a technicality.

    Kf

Sign In or Register to comment.