我已经编写了将数据插入SQLite
数据库的代码,当时我nullpointer
在行返回时遇到异常db.insert("userdetails", null, initialValues);
public class DBUserAdapter {
private static final String DATABASE_NAME = "users";
private static final int DATABASE_VERSION = 1;
private static final String USERDETAILS=
"create table userdetails(usersno integer primary key autoincrement,photo BLOB,date text not null);";
private Context context = null;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
// public DBUserAdapter(Context ctx) {
// this.context = ctx;
// DBHelper = new DatabaseHelper(context);
// }
public DBUserAdapter(SampleViewBase sampleViewBase) {
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(USERDETAILS);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}
public void open() throws SQLException
{
db = DBHelper.getWritableDatabase();
}
public void close()
{
DBHelper.close();
}
public long insert(byte[] photo, String date)
{
ContentValues initialValues = new ContentValues();
initialValues.put("photo", photo);
initialValues.put("date", date);
// initialValues.put(KEY_TIME, time);
Log.d("inotvaluessssssssss",initialValues.toString());
return db.insert("userdetails", null, initialValues);
}
我的主要活动
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private static final String TAG = "Sample-ImageManipulations::SurfaceView";
DBUserAdapter dbUser= null ;
private SurfaceHolder mHolder;
private VideoCapture mCamera;
byte[] photo;
// Mat mRgba;
Mat mRgba = new Mat();
Mat mRgba1= new Mat();
// private FpsMeter mFps;
int[] intArray = new int[50];
// int[] y;
// int check=0;
int cod=0;
public SampleViewBase(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
// mFps = new FpsMeter();
Log.i(TAG, "Instantiated new " + this.getClass());
}
public boolean openCamera() {
Log.i(TAG, "openCamera");
synchronized (this) {
releaseCamera();
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
if (!mCamera.isOpened()) {
mCamera.release();
mCamera = null;
Log.e(TAG, "Failed to open native camera");
return false;
}
}
return true;
}
public void releaseCamera() {
Log.i(TAG, "releaseCamera");
synchronized (this) {
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
}
public void setupCamera(int width, int height) {
Log.i(TAG, "setupCamera("+width+", "+height+")");
synchronized (this) {
if (mCamera != null && mCamera.isOpened()) {
List<Size> sizes = mCamera.getSupportedPreviewSizes();
int mFrameWidth = width;
int mFrameHeight = height;
// selecting optimal camera preview size
{
double minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - height) < minDiff) {
mFrameWidth = (int) size.width;
mFrameHeight = (int) size.height;
minDiff = Math.abs(size.height - height);
}
}
}
mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
}
}
}
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
Log.i(TAG, "surfaceChanged");
setupCamera(width, height);
}
public void surfaceCreated(SurfaceHolder holder) {
Log.i(TAG, "surfaceCreated");
(new Thread(this)).start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i(TAG, "surfaceDestroyed");
releaseCamera();
}
protected abstract Bitmap processFrame(VideoCapture capture);
public void run() {
Log.i(TAG, "Starting processing thread");
//mFps.init();
while (true) {
Bitmap bmp = null;
synchronized (this) {
if (mCamera == null) {
Log.i(TAG, "mCamera == null");
break;
}
if (!mCamera.grab()) {
Log.e(TAG, "mCamera.grab() failed");
break;
}
bmp = processFrame(mCamera);
String i1=bmp.toString();
dbUser = new DBUserAdapter(SampleViewBase.this);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local time in the system
// mRgba= (Mat)processFrame(mCamera);
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100 /*ignored for PNG*/, blob);
photo = blob.toByteArray();
dbUser.insert(photo, dateTime);
Log.d("bitmapdataaaaaaaaaaaaaaaaaaaaaaa",""+photo);
Bitmap bmp32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp32,mRgba);
Utils.bitmapToMat(bmp32,mRgba1);
// Log.d("lengthhhhhhhhhhhh",""+bitmapdata.length);
// for(int k=0;k<bitmapdata.length;k++)
// {
//
// }
bmp = processFrame(mCamera);
int i= bmp.getWidth();
int j=bmp.getHeight();
Log.d("Coming outoutttttttttttt ","Coming outputtttttttt");
intArray=FindFeatures(mRgba1.getNativeObjAddr(),mRgba.getNativeObjAddr());
Log.d("valueeeeeeeeee",""+intArray[0]);
Log.d("valueeeeeeeeee",""+intArray[1]);
Log.d("valueeeeeeeeee",""+intArray[2]);
Log.d("valueeeeeeeeee",""+intArray[3]);
Log.d("valueeeeeeeeee",""+intArray[3]);
// Log.d("Coming outo-----------------------==========","hjhjhj");
// mRgba=(Mat)bitmapdata;
// mFps.measure();
// Utils.matToBitmap(mRgba, bmp);
}
if (bmp != null)
{
Canvas canvas = mHolder.lockCanvas();
if (canvas != null)
{
//Log.d("Coming outo---454545--------------------==========","hjhjhj");
canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()), null);
// mFps.draw(canvas, (canvas.getWidth() - bmp.getWidth()) / 2, 0);
mHolder.unlockCanvasAndPost(canvas);
}
bmp.recycle();
}
//FindFeatures(bitmapdata,j,i);
}
Log.i(TAG, "Finishing processing thread");
}
// public native void FindFeatures(final byte[] bitmapdata,long m,int height,int width);
public native int[] FindFeatures(long n,long m);
static {
// Log.d("Coming hereeeeeee","Coming hereeeeeeeeeeeeee");
System.loadLibrary("mixed_sample");
}
public class Demo
{
int[] x;
int[] y;
int check=0;
int cod=0;
}
}