android4.0 USB Camera Instance (iii) UVC

Source: Internet
Author: User
Tags log e

I wrote two articles in front to illustrate the zc301 of the implementation of the specific see http://blog.csdn.net/hclydao/article/details/21235919

The following by the way the general USB is also written on the front of the ZC301 get to the format is JPEG so do not have to do what processing can be used to display, but the standard UVC gets to the YUV422 format needs to be converted to RGB before it can be displayed here we in the previous two code slightly modified on it

The HAL layer code is as follows

/* * Android USB Camera zc3xx Library * * Copyright (c) The Store information technology Guangzhou LtdThere are two main functions added

Yuyv422toabgry This is YUV turn RGB

Java_com_dao_usbcam_fimcgzsd_pixeltobmp This is the RGB turn bitmap

These are the two functions I found on the Internet, Simplewebcam.

At the same time Android.mk modified as follows

Local_path: = $ (call My-dir) include $ (clear_vars) local_module    : = fimcgzsdlocal_src_files: = Fimcgzsd.clocal_ Ldlibs    : =-llog-ljnigraphicsinclude $ (build_shared_library)
Added a application.mk

# The ARMV7 is significanly faster due to the use of the hardware Fpuapp_abi: = Armeabi armeabi-v7aapp_platform: = Android -8
This allows the terminal to enter the project all directories directly execute Ndk-build (environment variable has been set) can generate dynamic library

Application Layer

Add a C_type Specify whether it is zc301 or UVC I'll just stick the code up.

Package Com.dao.usbcam;import Java.io.bytearrayoutputstream;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.graphics.imageformat;import Android.graphics.rect;import Android.graphics.yuvimage;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.view.onclicklistener;import Android.view.Window; Import Android.view.windowmanager;import Android.widget.button;import Android.widget.imageview;import Android.widget.toast;public class Mainactivity extends Activity {private ImageView mimag;private int width = 320;private I NT height = 240;private String devname = "/dev/video3";p rivate byte[] mdata;private Handler mhandler;private int numbuf = 0;private int index = 0;private int ret = 0;privateint ctype = 1;//0 is zc301 1 is UVC camerapublic Button mcap;private Bitmap bitmap;private Bitmap bmp;private int[] Rgb;@o verrideprotected 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); bmp = Bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888) Numbuf = 4;mdata = new Byte[width * Height * Numbuf];rgb = new Int[width * Height * numbuf];ret = Fimcgzsd.open (Devname.getbytes ()); if (Ret < 0) finish (); ret = fimcgzsd.init (width, height, numbuf,ctype); 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 () {@Overridepublic void RUn () {//TODO auto-generated method Stubmimag.setimagebitmap (bitmap); }};class Startthread extends Thread {@Overridepublic void Run () {//TODO auto-generated method Stub//super.run (); while (tr UE) {if (CType = = 1) {index = FIMCGZSD.DQBUF (mdata); if (Index < 0) | | (Mdata = = null)) {OnDestroy (); break;} Fimcgzsd.pixeltobmp (BMP); Mhandler.post (mupdateui); bitmap = bmp; FIMCGZSD.QBUF (index);//fimcgzsd.yuvtorgb (Mdata, RGB);//mhandler.post (mupdateui);//bitmap = Bitmap.createbitmap ( rgb,width,height,bitmap.config.argb_8888);//fimcgzsd.qbuf (index);} else {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 + M        Time.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, FOut);                Fout.flush ();        Fout.close ();        } catch (FileNotFoundException e) {e.printstacktrace ();        } catch (IOException e) {e.printstacktrace (); }} class Capturelistener implements onclicklistener{@Overridepublic void OnClick (View v) {//TODO auto-generated Me Thod stub//new Capthread (). Start ();//fimcgzsd.streamoff (); Savemybitmap (bitmap);//fimcgzsd.streamon ();    Toast.maketext (Mainactivity.this, "Capture successfully", Toast.length_short). Show (); } class Capthread extends Thread {@Overridepublic void Run () {//TODO auto-generated method Stub//super.run (); Savemybitmap (bitmap); Toast.maketext (Mainactivity.this, "Capture successfully", Toast.length_long). Show ();}} @Overrideprotected void OnStop () {//TODO auto-generated method Stubsuper.onstop (); Fimcgzsd.release (); Finish ();} @Overrideprotected void OnPause () {//TODO auto-generated method Stubsuper.onpause (); Fimcgzsd.release (); Finish ();} @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); Fimcgzsd.release ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); Finish (); return true;}}
I've already made a simple statement here, and I'm not going to say it. Specific effects are as follows

It's a little blurry. I found that the effect of self-portrait is also good fuzzy produce beautiful haha.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.