2013年10月1日 星期二

安裝 Processing for Android

1.先到Android Developer官網下載SDK工具包,解壓縮後執行/tools/android

2.再到Processing官網下載Processing,覺得好用的話,下次下載時可考慮捐點錢

3.解壓縮後,執行./processing,此時只支援標準的Java mode

4.在Mode Manager裡,選擇Install Android Mode

5.順利的話,幾分鐘就可安裝完畢

6.回到主畫面,準備切換到Android mode

7.在Linux,Processing可能會找不到Android SDK,會詢問是否手動加入Android SDK路徑

8.選擇開頭下載SDK工具包,解壓縮後的檔案路徑

2013年7月15日 星期一

[ Processing for Android ] A Camera example for prior ICS

ketai library目前提供的KetaiCamera範例應該只適用於Android ICS之後的版本,如果想在ICS之前的版本執行的話,在http://www.akeric.com/blog/?p=1342提供一份很詳細的原始碼範例,不過畢竟作者是在2010年寫出的,有一小部份的code已不適用目前的Processing for Android 2.x版本,所以我稍微調整了幾行code,就可以在舊平板上跑起來

import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import android.content.Context; 
import android.hardware.Camera; 
import android.hardware.Camera.PreviewCallback; 
import android.view.*;

import java.util.*; 
import java.io.*; 

public class camera extends PApplet {


CameraSurfaceView gCamSurfView;

PImage gBuffer;

public void setup() {
  //用不到
}

public void draw() {
  //用不到
}


public void onResume() {
  super.onResume();
  orientation(LANDSCAPE);
  gCamSurfView = new CameraSurfaceView(this.getApplicationContext());
}


class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {

  SurfaceHolder mHolder;
  Camera camera = null;
  Camera.Size prevSize;

  CameraSurfaceView(Context context) {
    super(context);

    mHolder = getSurfaceHolder();

    mHolder.addCallback(this);
  }

  public void surfaceCreated (SurfaceHolder holder) {

    camera = Camera.open();
    camera.setPreviewCallback(this);
    
    
    Camera.Parameters parameters = camera.getParameters();
    parameters.setPreviewSize(800,800); 
    camera.setParameters(parameters);


    prevSize = parameters.getPreviewSize();
    gBuffer = createImage(prevSize.width, prevSize.height, RGB);
  }  

  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    camera.startPreview();
  }

  public void surfaceDestroyed (SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
    camera = null;
  }


  public void onPreviewFrame(byte[] data, Camera cam) {
    gBuffer.loadPixels();
    decodeYUV420SP(gBuffer.pixels, data, prevSize.width, prevSize.height);
    gBuffer.updatePixels();
    image(gBuffer, 0, 0);
  }

  public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {
    // 原作者改自 Pulled directly from:
    // http://ketai.googlecode.com/svn/trunk/ketai/src/edu/uic/ketai/inputService/KetaiCamera.java
    final int frameSize = width * height;

    for (int j = 0, yp = 0; j < height; j++) {       int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
      for (int i = 0; i < width; i++, yp++) {
        int y = (0xff & ((int) yuv420sp[yp])) - 16;
        if (y < 0)
          y = 0;
        if ((i & 1) == 0) {
          v = (0xff & yuv420sp[uvp++]) - 128;
          u = (0xff & yuv420sp[uvp++]) - 128;
        }

        int y1192 = 1192 * y;
        int r = (y1192 + 1634 * v);
        int g = (y1192 - 833 * v - 400 * u);
        int b = (y1192 + 2066 * u);

        if (r < 0)
           r = 0;
        else if (r > 262143)
           r = 262143;
        if (g < 0)
           g = 0;
        else if (g > 262143)
           g = 262143;
        if (b < 0)
           b = 0;
        else if (b > 262143)
           b = 262143;

        rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
      }
    }
  }
}

  public int sketchWidth() { return displayWidth; }
  public int sketchHeight() { return displayHeight; }
}

2013年7月2日 星期二

[ Processing for Android ] A Simple TouchMotion example

import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import android.view.MotionEvent; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.*;

public class TestTouchMotion extends PApplet {

public void setup() {
 

  orientation(PORTRAIT);
  stroke(255);
  smooth();

}

public void draw() {
  background(0);
}

public void showCircle(float x, float y, float siz) {
  int circleSize = 500;
  float diameter = circleSize * siz;
  noFill();
  strokeWeight(20);
  colorMode(RGB, 255);
  stroke(PApplet.parseInt(random(0,255)),PApplet.parseInt(random(0,255)),PApplet.parseInt(random(0,255)));
  ellipse(x, y, diameter, diameter);
  fill(0,255,0);
  ellipse(x, y, 8, 8);
}

public boolean dispatchTouchEvent(MotionEvent me) {
  int numPointers = me.getPointerCount();
  for(int i=0; i < numPointers; i++) {
    int pointerId = me.getPointerId(i);
    float x = me.getX(i);
    float y = me.getY(i);
    float siz = me.getSize(i);
    showCircle(x, y, siz);
  }
  return super.dispatchTouchEvent(me);
}


  public int sketchWidth() { return displayWidth; }
  public int sketchHeight() { return displayHeight; }
}

2013年6月29日 星期六

[ Processing for Android ] A Simple Thread example

package processing.test.threadexample;

import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.*;

public class ThreadExample extends PApplet {



// A Simple Thread example


CountdownThread a_thread;
CountdownThread b_thread;
CountdownThread c_thread;

public void setup() {
  orientation(PORTRAIT);
 
  textSize(35);
  a_thread = new CountdownThread(1000,10);
  a_thread.start();
  
  b_thread = new CountdownThread(1000,100);
  b_thread.start();
  
  c_thread = new CountdownThread(1000,1000);
  c_thread.start();

}

public void draw() {
  
  background(255);
  fill(0);
  
  int a = a_thread.getCount();
  text("Count down : "+a,10,50);
  
  int b = b_thread.getCount();
  text("Count down : "+b,10,200);
  
  int c = c_thread.getCount();
  text("Count down : "+c,10,350);  
}

class CountdownThread extends Thread {
  
    int wait;   // How many milliseconds
    int count;  // counter
    
    CountdownThread (int w, int c) {
    wait = w;
    count = c;
  }
  
    public int getCount() {
    return count;
  }
  
    // Overriding "start()"
    public void start () {
    super.start();
  }
  
    public void run () {
    while (count > 0) {
      count--;
      try {
        sleep((long)(wait));
      } catch (Exception e) {
      }
    }
    quit();
  }
  
  // the method that quits the thread
  public void quit() {
    interrupt();
  }
}

  public int sketchWidth() { return 500; }
  public int sketchHeight() { return 500; }
}

2013年6月22日 星期六

[ Processing for Android ] fisica 2D 物理系統

import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import fisica.*; 

import java.util.*; 
import java.io.*;  

public class Pipe extends PApplet {

FWorld world;

public void setup() {
 
  smooth();

  Fisica.init(this);

  world = new FWorld();
  world.setGravity(0, -300);


  FPoly l = new FPoly();
  l.vertex(width/4, 0);
  l.vertex(0, 0);
  l.vertex(0, height);

  l.vertex(width/4, height);
  l.vertex(width/4, 7*height/10);
  l.vertex(width/2, 7*height/10);
  l.vertex(width/2, 3*height/10);
  l.vertex(width/4, 3*height/10);
  l.setStatic(true);
  l.setFill(0);
  l.setFriction(0);
  world.add(l);

  FPoly r = new FPoly();
  r.vertex(width/4+60, 0);
  r.vertex(width, 0);
  r.vertex(width, height);

  r.vertex(width/4+60, height);
  r.vertex(width/4+60, 7*height/10+60);
  r.vertex(width/2+60, 7*height/10+60);
  r.vertex(width/2+60, 3*height/10-60);
  r.vertex(width/4+60, 3*height/10-60);
  r.setStatic(true);
  r.setFill(0);
  r.setFriction(0);
  world.add(r);
}

public void draw() {
  background(80, 120, 200);

  if ((frameCount % 40) == 1) {
    FBlob b = new FBlob();
    float s = random(30, 40);
    b.setAsCircle(width/4+20, height-random(100), s, 20);
    
    b.setStroke(0);
    b.setStrokeWeight(2);
    b.setFill(255);
    b.setFriction(0);
    world.add(b);
  }

  world.step();
  world.draw();
}

  public int sketchWidth() { return 400; }
  public int sketchHeight() { return 400; }
}

2013年6月9日 星期日

第一支Processing for Android 程式

package processing.test.radiation;

import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class Radiation extends PApplet {

String lines[]; 
String [][] radiation_csv;
int csvWidth=0;

public void setup() {
lines = loadStrings("http://www.aec.gov.tw/open/gammamonitor.csv");
background(255);
textSize(16);

for (int i=0; i < lines.length; i++) {
  String [] chars=split(lines[i],',');
  if (chars.length>csvWidth){
    csvWidth=chars.length;
  }
}


radiation_csv = new String [lines.length][csvWidth];


for (int i=0; i < lines.length; i++) {
  String [] temp = new String [lines.length];
  temp= split(lines[i], ',');
  for (int j=0; j < temp.length; j++){
      radiation_csv[i][j]=temp[j];
  }
}

}

public void draw() {
for (int i=1; i < lines.length; i++) {
   fill(0, 102, 153, 204);
   text(radiation_csv[i][1]+" "+radiation_csv[i][2]+" (mSv/hr)",10,20+i*15);
   stroke(255, 0, 0);
   line(200, 20+i*15, 200+PApplet.parseFloat(radiation_csv[i][2])*2000, 20+i*15);

  }

}

  public int sketchWidth() { return 800; }
  public int sketchHeight() { return 800; }
}

2013年5月2日 星期四

F-droid,一個開放原始碼的Android APP市集

怕下載的Android APP內含惡意程式碼?
或許你可以到 F-droid下載看看,若還是不放心,還可以下載原始程式碼,重新編譯