You have to set your img to your ketaivideo image Object.
in draw
img = video;
You have to set your img to your ketaivideo image Object.
in draw
img = video;
im not sure exactly how the video part in ketai is handled, but looking at your code…
video.read();
image(video, 0, 0, width, height);
video.loadPixels();
video seems to be an PImage object.
the code work on android same as work on with pc video library but without save yet
please look at the code you should be able to figure it out from my previous comments.
ps the save function can take an image.
currently in the code
if(success){
save(file+"/"+tf);
image_saved = true;
needs to be
if(success){
video.save(file+"/"+tf);
image_saved = true;
@paulgoux thank you again i am Failed
@paulgoux hi
This code works and save but there is a strange thing each time I run it gives me the last time captured image I don’t know why do you have explanation?
import android.os.Environment;
import ketai.camera.*;
KetaiCamera video;
PrintWriter output;
//colors
color black=color(0);
color white=color(255);
//variables
int iteration;
float pics_per_rev = 20;
String directory_path;
void setup() {
orientation(LANDSCAPE);
video= new KetaiCamera(this, 600, 480, 60);
// println(video.list()); // 1
// 0: back camera; 1: front camera
video.setCameraID(0); // 2
imageMode(CENTER);
// 3
video.start();
}
void draw() {
// image(video, width/2, height/2, width, height);
drawUI(); // 4
}
void drawUI() { // 1
}
void onCameraPreviewEvent() {
// video.start();
video.read();
image(video, 0, 0, width, height);
video.loadPixels();
delay(4000);
for (iteration=0; iteration<pics_per_rev; iteration++) {
video.read();
image(video, 0, 0, width, height);
video.loadPixels();
for (int n=0; n<video.width*video.height; n++) {
video.pixels[n]=video.pixels[n];
}
video.updatePixels();
set(20, 20, video);
String file ="raw_image-"+nf(iteration+1, 3)+".png";// this the orginal
//video.save(file_name);
String file_name= new String(Environment.getExternalStorageDirectory().getAbsolutePath() +"/go/");
video.save( file_name+ file);
noLoop();
}
}
It probably means you need to place the save bit in a different part of the code.
In any case im sure you’ll figure it out.
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
`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
`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
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
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.");
}
};
};
hi i have finished every thing and thanks a lot for you
every thing going fine in Preview mode but when convert to apk i stuck with one issue
String file_name="raw_image-"+nf(Repetition+1, 3)+".png";
PImage data =LoadImage ( file_name);
i could not load the image from the saved directory after browsing the net i knew that its path issues i saw a lot of codes but nothing work with me
do you have an idea you to load image from folder and thank you
can’t exactly say without knowing which bit of code you are referring to, ive posted a lot of examples.
if you can explain this code and how to use it
PImage imga;// the image which is inside the subfolder called "autredos" and you want to get
Activity act;
String finalPath;
PImage[] img;// arrayList for all images
void setup(){
size(600,400);
background(255);
act= this.getActivity();
img = loadImages("autredos"+"/"+"autresousdos");// for creating the list in the subfolder
imga = img[0];// here i suppose that i know the image i will display; in some cases you have to create a list view and can choose from it
}
void draw(){
background(255,0,0);
image(imga, 0, 0); //displaying the image imaga, in the subfolder
}
PImage[] loadImages(String folderName_){
AssetManager assetManager = act.getAssets();
try{
String[] imgPath = assetManager.list(folderName_);
println(imgPath.length);
File[] files = new File[imgPath.length];
img = new PImage[imgPath.length];
for(int i=0; i<imgPath.length; i++){
println (imgPath[i]);
img[i] = loadImage(folderName_ + "/" + imgPath[i]);
files[i] = new File(folderName_+"/" + imgPath[i]);//pour des fichiers quelconque, here you can find all folders
println("file=====" + files[i]);
}
}
catch(IOException ex){
System.out.println (ex.toString());
}
return img;
}
Unfortunately its not that easy to retrieve images from android. There are a few image pickers floating around on the site but nothing that will allow you to read images from a url string. I’m currently looking into the issue.
see this link
import android.view.MotionEvent;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.app.Activity;
Intent intent;
void setup () {
size (displayWidth, displayHeight, JAVA2D);
colorMode (RGB, 256);
background (255);
//Android端末のファイル保存環境を取得
intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
}
void draw () {
fill (random (256), random (256), random (256));
ellipse (random (width), random (height), 20, 20);
}
void mouseReleased () {
//Android端末に画像を保存
save_image ();
}
void save_image () {
String img_name = Environment.getExternalStorageDirectory() + "/test_image.png";
save (img_name);
sendBroadcast (intent);
//保存した後プリントアウト
print_out (img_name);
}
void print_out (String img_path) {
Intent shareIntent = new Intent (Intent.ACTION_SEND);
shareIntent.setType ("image/png");
shareIntent.putExtra (Intent.EXTRA_STREAM, Uri.fromFile (new File (img_path)));
startActivity (shareIntent);
}
import android.view.MotionEvent;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.app.Activity;
Intent intent;
void setup () {
size (displayWidth, displayHeight, JAVA2D);
colorMode (RGB, 256);
background (255);
//Android端末のファイル保存環境を取得
intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
}
void draw () {
fill (random (256), random (256), random (256));
ellipse (random (width), random (height), 20, 20);
}
void mouseReleased () {
String img_name = Environment.getExternalStorageDirectory() + "/test_image.png";
save (img_name);
sendBroadcast (intent);
}
saving an image we can already do, however reading an image from a string is slightly trickier.
Anyways just finished the code.
import android.content.Context;
import android.app.Activity;
import android.content.res.AssetManager;
import java.net.MalformedURLException;
import android.app.Activity;
import android.content.Intent;
import processing.core.PApplet;
import android.os.Bundle;
import android.os.Environment;
import android.content.Context;
import android.provider.MediaStore;
import android.net.Uri;
import android.graphics.BitmapFactory;
import android.database.Cursor;
import android.os.Build.VERSION_CODES;
import android.os.Build;
import android.graphics.Bitmap;
import java.net.URL;
import android.graphics.Bitmap;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import android.os.Environment;
import android.graphics.BitmapFactory;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Build.VERSION_CODES;
Activity act;
String finalPath;
ArrayList<PImage> images= new ArrayList<PImage>();
PImage[] img;
PImage image;
Permission wStorage;
ArrayList<File> Files = new ArrayList<File>();
String absolutePath;
Context context;
void setup() {
size(displayWidth, displayHeight);
//background(255);
absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
act = this.getActivity();
context = act.getApplicationContext();
wStorage = new Permission(this, "WRITE_EXTERNAL_STORAGE");
img = loadImages("notes");
};
void draw() {
background(0);
if (0<images.size()&&images.get(0)!=null)image(images.get(0), 0, 0);
else if(frameCount%60==0)println("no images found",images.size());
};
void mousePressed() {
loadImages("notes");
};
PImage[] loadImages(String folderName_) {
act= this.getActivity();
AssetManager assetManager = act.getAssets();
try {
String[] imgPath = listFiles(absolutePath+"/"+folderName_);
img = new PImage[imgPath.length];
for (int i=0; i<imgPath.length; i++) {
if(imgPath[i].contains("jpg")){
File f = new File(absolutePath+"/"+folderName_ + "/" + imgPath[i]);
Uri image_uri= Uri.fromFile(f);
// = new URL();
InputStream ips = context.getContentResolver().openInputStream(image_uri);
Bitmap bitmap = BitmapFactory.decodeStream(ips);
if(bitmap!=null){
image = new PImage(bitmap.getWidth(), bitmap.getHeight(), PConstants.ARGB);
image.loadPixels();
bitmap.getPixels(image.pixels, 0, image.width, 0, 0, image.width, image.height);
image.updatePixels();
//img[i] = image;
images.add(image);
;
println(imgPath[i]);
}}
}
}
catch(ArrayIndexOutOfBoundsException e) {
println("no file");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
};
public String[] listFiles(String s1) {
String []s = null;
String path = s1;
println("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
if (files!=null) {
s = new String [files.length];
println("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
s[i] = files[i].getName();
Files.add(new File(s1+s[i]));
println("Files", "FileName: " + files[i].getName());
}
}
println("Images", images.size());
return s;
};
dont forget permissions
public class Permission{
PApplet parent;
String p;
public Permission(PApplet pParent,String permissionName) {
parent = pParent;
p = permissionName;
parent.requestPermission("android.permission."+permissionName, "onPermissionResult", this);
};
public void onPermissionResult(boolean granted) {
if (!granted) {
PApplet.println("User did not grant", p ,"permission.", p ,"is disabled.");
}
};
};
you set the folder destination in loadImage. You’ll have to add other image file types if you want to open something other than jpg.
In any case I also feel this is getting off topic.
Perhaps you ought to start a new thread.
hi
so here my folder path img = loadImages(“notes”); instead of notes ???
then how to put this part and where ?
String file_name="raw_image-"+nf(Repetition+1, 3)+".png";
PImage data =LoadImage ( file_name);