Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 4.0 USB Camera實例(二)應用層

android 4.0 USB Camera實例(二)應用層

編輯:關於Android編程

上一篇說了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直接顯示 又省了不少事

貼上我的效果圖

30W像素的攝像頭,目前capture似乎還有問題,似乎是驅動有問題 繼續研究中...

============================================
作者:hclydao
http://blog.csdn.net/hclydao
版權沒有,但是轉載請保留此段聲明

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



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved