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月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,就可以在舊平板上跑起來
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言