android 4.0 USB Camera实例(二)应用层

上一篇说了HAL层 这一篇具体应用HAL层 实例预览与拍照的功能

首先贴上两个主文件的源码

Fimcgzsd.java

package com.dao.usbcam;

public class Fimcgzsd {
	static public native int open(byte[] devname);
	static public native int qbuf(int index);
	static public native int streamon();
	static public native int streamoff();
	static public native int dqbuf(byte[] videodata);
	static public native int release();
	static public native int init(int width, int height,int numbuf);
	static {
		System.loadLibrary("fimcgzsd");
	}
}
这个主要是HAL层的一些接口函数

MainActivity.java

package com.dao.usbcam;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.text.format.Time;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ImageView mImag;
	private int width = 640;
	private int height = 480;
	private String devname = "/dev/video3";
	private byte[] mdata;
	private Handler mHandler;
	private int numbuf = 0;
	private int index = 0;
	private int ret = 0;
	public Button mcap;
	private Bitmap bitmap;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		  WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);
		mImag = (ImageView)findViewById(R.id.mimg);
		mcap = (Button)findViewById(R.id.mcap);
		numbuf  = 4;
		mdata = new byte[width * height * numbuf];
		ret = Fimcgzsd.open(devname.getBytes());
		if(ret < 0)
			finish();
		ret = Fimcgzsd.init(width, height, numbuf);
		if(ret < 0)
			finish();
		ret = Fimcgzsd.streamon();
		if(ret < 0)
			finish();
		mHandler = new Handler();
		new StartThread().start();
		mcap.setOnClickListener(new CaptureListener());
	}

	final Runnable mUpdateUI = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			mImag.setImageBitmap(bitmap);
		    
		}
	};
	
	class StartThread extends Thread {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			//super.run();
			while(true) {
				index = Fimcgzsd.dqbuf(mdata);
				if(index < 0) {
					onDestroy();
					break;
				}
				mHandler.post(mUpdateUI);
				bitmap = BitmapFactory.decodeByteArray(mdata, 0, width * height);
				Fimcgzsd.qbuf(index);
			}
		}

	}
	public static void saveMyBitmap(Bitmap mBitmap) {
    	Time mtime = new Time();
    	mtime.setToNow();
    	File fdir = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + "/gzsd/");
    	if(!fdir.exists()) {
    		fdir.mkdir();
    	}
        File f = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + "/gzsd/" + mtime.year + mtime.month + mtime.monthDay + mtime.hour + mtime.minute +mtime.second+".png");
        try {
                f.createNewFile();
        } catch (IOException e) {
        	e.printStackTrace();
        }
        FileOutputStream fOut = null;
        try {
                fOut = new FileOutputStream(f);
                mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();
        } catch (FileNotFoundException e) {
        	e.printStackTrace();
        } catch (IOException e) {
        	e.printStackTrace();
        }

    }
	
    class CaptureListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//new CapThread().start();
			//Fimcgzsd.streamoff();
			saveMyBitmap(bitmap);
			//Fimcgzsd.streamon();
			Toast.makeText(MainActivity.this, "Capture Successfully", Toast.LENGTH_LONG).show();
		}
    	
    }
    
	class CapThread extends Thread {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			//super.run();
			saveMyBitmap(bitmap);
			Toast.makeText(MainActivity.this, "Capture Successfully", Toast.LENGTH_LONG).show();
		}
		
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Fimcgzsd.release();
		finish();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Fimcgzsd.release();
		finish();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Fimcgzsd.release();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		finish();
		return true;
	}

}
整体过程 其实很简单

首先open

这里usb camera设备为/dev/video3 为了修改方面所以我把这个参数放在应用层里传到HAL层 分辨率为640*480

然后init

Fimcgzsd.init(width, height, numbuf);
传入宽,高 以及 缓冲帧的数目

然后开启视频流

Fimcgzsd.streamon();
开另外一个进程 循环获取数据 由于这里获取到的数据直接是jpeg格式的所以 我直接把他转成bmp然后用imageview直接显示 又省了不少事

贴上我的效果图

android 4.0 USB Camera实例(二)应用层

30W像素的摄像头,目前capture似乎还有问题,似乎是驱动有问题 继续研究中...

============================================
作者:hclydao
http://blog.csdn.net/hclydao
版权没有,但是转载请保留此段声明

============================================



android 4.0 USB Camera实例(二)应用层,布布扣,bubuko.com

android 4.0 USB Camera实例(二)应用层

上一篇:[Android] android:visibility属性应用


下一篇:Android利用soap WSDL与Webservice通信