made some minor tweaks to the image saver class.
@jafal fiy I was hoping you would be able to use the image saver code I’d left to produce this solution.
now there were a few updates had to make to the code. My previous code would max frameRate at 10 fps, as it was always testing the save location first.
So checkLocation is called only once now in setup.
Then method logic2()
update from logic()
just takes the initial folder name and filename add a counter after the first save.
increments counter after every save this way you do not overwrite all your previous files.
Things to bear in mind. The cam stream I believe produces on 15 fps, whilst processing runs at 60 fps so you might get a lot of repetition in your frames alternatively you could test if your current frame is the same as the last this shouldnt cause too much problems in terms of speed as you should have 3 free framed to work with.
the following code should open a video stream on android and save the frames as a file of your choice in a folder of your choice.
Please note that I havent bothered to add functionality for deep folder structures. Though this could be added easily later. But its important to keep in mind.
Please refer to my previous posts on imagesaver for android if you want the basic code.
sketch file
Summary
`import android.os.Environment;
import android.os.Build ;
import android.app.Activity;
import android.content.Context;
boolean k1 = false;
AndroidCamera cam;
imageSaver output;
void setup() {
fullScreen(P2D);
orientation(LANDSCAPE);
cam = new AndroidCamera(this, width, height, 60);
output = new imageSaver(this);
output.checkLocation(“cameraStills”,“img.jpg”);
//output.folderName = “cameraStills”;
//output.fileName = “img.jpg”;
};
void draw() {
fill(255);
background(0);
cam.display();
output.logic2(cam.canvas);
};
void mousePressed() {
cam.toggleFlash();
};
void onCameraPreviewEvent() {
cam.read();
};`
camera class
Summary
`class AndroidCamera {
PApplet applet;
KetaiCamera cam;
int w, h;
Permission storage, camera, readStorage, P;
PGraphics canvas;
PShader shader;
AndroidCamera(PApplet p, int w, int h, int frate) {
this.w = w;
this.h = h;
applet = p;
cam = new KetaiCamera(p, w, h, frate);
P = new Permission(p, “WRITE_EXTERNAL_STORAGE”);
P = new Permission(p, “READ_EXTERNAL_STORAGE”);
P = new Permission(p, “CAMERA”);
canvas = p.createGraphics(w, h, P2D);
};
void display() {
start();
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);
if (shader!=null)canvas.shader(shader);
canvas.image(cam, 0, 0);
canvas.text(frameRate, 50, 100);
applet.image(canvas, 0, 0);
//canvas.imageMode(CORNER);
//canvas.fill(0);
//canvas.rect(0, 0, width, 20);
canvas.endDraw();
};
void start() {
if (applet.frameCount>10&&!cam.isStarted())cam.start();
};
void toggleFlash() {
if (cam.isStarted()) {
if (cam.isFlashEnabled())
cam.disableFlash();
else
cam.enableFlash();
}
};
void read() {
cam.read();
};
};`
image saver class
Summary
class imageSaver{
Permission wstorage,rstorage;
Activity act;
PApplet p;
int counter = -1,maxImageCount;
boolean imageSaved,mdown;
String folderName = "MyImageFolder",fileName,ext,absolutePath;
String imageFile = "MyImage.jpg";
PImage img;
boolean EXT;
File file,file2;
imageSaver(PApplet applet){
p = applet;
wstorage = new Permission(p,"WRITE_EXTERNAL_STORAGE");
act = p.getActivity();
//getExt(imageFile);
};
imageSaver(PApplet applet,String s1, String s2){
folderName = s1;
imageFile = s2;
p = applet;
wstorage = new Permission(p,"WRITE_EXTERNAL_STORAGE");
act = p.getActivity();
//getExt(imageFile);
};
public void logic() {
if(mousePressed&&!mdown) {
//img = p.get();
//img = cam.cam;
checkLocation(folderName, imageFile);
mdown = true;
counter++;
}
if(!mousePressed)mdown = false;
};
public void logic(PGraphics canvas) {
if(true) {
img = canvas.get();
checkLocation(folderName, imageFile);
counter++;
}
};
public void logic(PImage canvas) {
if(true) {
img = canvas;
checkLocation(folderName, imageFile);
counter++;
}
};
public void logic2(PImage canvas){
println(file);
if(counter>=0){
file = new File(absolutePath+"/"+folderName+"/"+fileName+counter+"."+ext);
canvas.save(file.getAbsolutePath());
}
else {
file = new File(absolutePath+"/"+folderName+"/"+fileName+"."+ext);
canvas.save(file.getAbsolutePath());
}
counter ++;
};
public void logic2(){
file = new File(absolutePath+"/"+folderName+"/"+fileName+counter+"."+ext);
file.mkdirs();
img.save(file+"/"+fileName+counter+"."+ext);
};
public void checkLocation(String sf, String tf) {
folderName = sf;
fileName = tf;
if(!EXT)getExt(tf);
boolean k = false;
try {
absolutePath = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
file = new File(absolutePath+"/"+sf);
//PApplet.println("checking file1",file,counter);
if (!file.exists()&&counter==-1) {
boolean success = true;
success = file.mkdirs();
img.save(file+"/"+fileName+"."+ext);
} else k = true;
if(counter>-1)k=true;
boolean k1 = false;
while(k&&counter<100&&!k1) {
try {
file2 = new File(absolutePath+"/"+folderName+"/"+fileName+counter+"."+ext);
PApplet.println("checking file2",file2);
if (file2.exists()) {
counter++;
} else {
k = false;
k1 = true;
break;
}
}
catch (Exception e) {
PApplet.println("Error while saving file: " + e);
}
}
if(k1){
//PApplet.println("file",file);
//PApplet.println("Fname",folderName);
//PApplet.println("fileName",fileName);
//PApplet.println("counter",counter);
//PApplet.println("ext",ext);
if(counter>-1)img.save(file+"/"+fileName+counter+"."+ext);
imageSaved = true;
//PApplet.println("File saved successfully.");
}
}
catch (Exception e) {
PApplet.println("Error while saving file: " + e);
}
};
void getExt(String location){
int count = 0;
fileName = location.substring(0,location.indexOf("."));
ext = location.replace(fileName,"");
ext = ext.replace(".","");
ext = ext.replace(fileName,"");
EXT = true;
};
public File[] listFiles(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
File[] files = file.listFiles();
return files;
} else {
// If it's not a directory
return null;
}
};
};
permission class
Summary
public class Permission{
PApplet parent;
String p;
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.");
}
};
};