Dynamic get(); method in Ketai library?

Hello,

I’m new in Processing, and I try to create some funny pixelator app on Android APDE using camera on my Phone. To solve this task I’m using a Ketai library. Problem is when I try to get image from camera using get() method^ I got only one frame as an image, not a video stream from my camera. But I need a realtime video stream from my camera. Who can help me to understand how can I do it. The code is here:

import ketai.camera.*;

KetaiCamera cam; 

color bg = #f1f1f1;
color fg = #111111;
PImage img;

void setup() { 
  requestPermission("android.permission.WRITE_EXTERNAL_STORAGE", "checkPermission");
  size(displayWidth, displayHeight);
  orientation(LANDSCAPE); 
  imageMode(CENTER); 
  cam = new KetaiCamera(this, displayWidth, displayHeight, 24);
  cam.setCameraID(0);
  background(bg);
} 


void draw() { 

    background(bg);
    fill(fg);

    if (mousePressed == true) {
      cam.start();
    } else {
      cam.stop();
    }
    
    img = cam.get();
    

    float tileX = map(mouseY, 0, height, 10, 100);
    //float tileY = ratio * tileX;
    float tileSize = width / tileX;

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

        color c = img.get(x, y);
        float b = map(brightness(c), 0, 255, 1, 0);

        pushMatrix();
        translate(x, y);
        rect(0, 0, b * tileSize, b* tileSize);
        popMatrix();
      }
    }
}

void checkPermission(boolean wasPermissionGranted) { 
  if (wasPermissionGranted) println("Hooray! I can now write to the local file system!"); 
  else println("Oh no! I was not granted write permission =(");
}


void onCameraPreviewEvent() { 
  cam.read();
}



// start/stop camera preview by tapping the screen
/*
void mousePressed() { 
 if (cam.isStarted()) { 
 cam.stop();
 } else cam.start();
 } 
 */

void keyPressed() { 
  if (key == CODED) { 
    if (keyCode == MENU) { 
      if (cam.isFlashEnabled()) cam.disableFlash(); 
      else cam.enableFlash();
    }
  }
}

Thank you)

simple ketai sketch

Summary
import android.os.Environment;
import android.os.Build ;
import android.app.Activity;
import android.content.Context;

boolean k1 = false;
int W = 1440, H = 720;
Permission storage, camera, readStorage;
PGraphics canvas;
PShader edges;
KetaiCamera cam;

//void settings(){
//  //size(W,H,P2D);
//};


void setup() {
  fullScreen(P2D);
  orientation(LANDSCAPE);
  cam = new KetaiCamera(this, width, height, 60);
  canvas = createGraphics(W, H, P2D);
  //edges = loadShader("edges.glsl");
  storage = new Permission(this, "WRITE_EXTERNAL_STORAGE");
  readStorage = new Permission(this, "READ_EXTERNAL_STORAGE");
  camera = new Permission(this, "CAMERA");
  //cam.start();
};

void draw() {
  fill(255);
  background(0);
  if (frameCount>10&&!cam.isStarted())cam.start();
  image(cam, 0,0);
  text(frameRate, 50, 100);
};

void mousePressed() {
  //if (cam.isStarted()){
  //  cam.stop();
  //}else cam.start();

  if (cam.isFlashEnabled())
    cam.disableFlash();
  else
    cam.enableFlash();
};

void onCameraPreviewEvent() {
  cam.read();
};
Summary
class AndroidAcamera {

  AndroidAcamera() {
  };


  void display() {
    //if(BMS.menus.get(0).items.get(1).toggle==1){
    canvas.beginDraw();

    //mult = BMS.Sliders.get(0).value;
    //counter = BMS.Sliders.get(1).value;
    //mult = 1.0;
    //counter = 1.0;
    //edges.set("mult",mult);
    //edges.set("type",counter);
    canvas.shader(edges);
    canvas.fill(0);
    canvas.rect(0, 0, width, height);
    canvas.imageMode(CENTER);
    canvas.image(cam, width/2, height/2+20);
    canvas.imageMode(CORNER);
    canvas.fill(0);
    canvas.rect(0, 0, width, 20);
    canvas.endDraw();
    //}
  };
  
  void start(){
    if (frameCount>10&&!cam.isStarted())cam.start();
  };
};
Summary
public class Permission{
  
  PApplet parent;
  String p;
  
  public boolean requestedPortraitImage = false;

  public Permission(PApplet pParent,String permissionName) {
    parent = pParent;
    p = permissionName;
    parent.requestPermission("android.permission."+permissionName, "onPermissionResult", this);
    println(permissionName);
  };

  public void onPermissionResult(boolean granted) {
    if (!granted) {
      PApplet.println("User did not grant ",p, "permission. Camera is disabled.");
    }
  };

};

Hello, please can you explain your solution in few words

1 Like

Just take it one line at a time my friend.