Android uses a camera to take photos

Source: Internet
Author: User
Tags key case
// Main. XML is as follows: <? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <surfaceview Android: id = "@ + ID/surfaceview" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"/> </linearlayout> // mainactivity is as follows: Package COM. bravesoft; import Java. io. file; import Java. io. fileoutputstream; Im Port android. app. activity; import android. graphics. bitmap; import android. graphics. bitmap. compressformat; import android. graphics. bitmapfactory; import android. graphics. pixelformat; import android. hardware. camera; import android. hardware. camera. parameters; import android. hardware. camera. picturecallback; import android. OS. bundle; import android. OS. environment; import android. view. display; import android. view. Keyevent; import android. view. surfaceholder; import android. view. surfaceview; import android. view. window; import android. view. windowmanager;/*** pay attention to permissions ** <uses-Permission Android: Name = "android. permission. camera "/> <! -- Create and delete file permissions in sdcard --> <uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/> <! -- Write data permission to sdcard --> <uses-Permission Android: Name = "android. permission. write_external_storage "/> * @ author administrator **/public class testcameraactivity extends activity {private surfaceview; private camera; private Boolean ispriview; @ overridepublic void oncreate (bundle privilege) {super. oncreate (savedinstancestate); // remove the title bar this. requestwindowfeature (window. feature_no_t Itle); // getwindow () is displayed on the full screen (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); // setcontentview (R. layout. main); surfaceview = (surfaceview) findviewbyid (R. id. surfaceview); surfaceview. getholder (). settype (surfaceholder. surface_type_push_buffers); surfaceview. getholder (). setfixedsize (176,144); // sets the resolution surfaceview. getholder (). addcallback (New surfaceholder Callbackimpl (); // callback of surfaceview} class surfaceholdercallbackimpl implements surfaceholder. callback {@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {}@ overridepublic void surfacecreated (surfaceholder holder) {windowmanager = (windowmanager) testcameraactivity. this. getsystemservice (window_service); display = windowmanager. getdefaultd Isplay (); camera = camera. open (); // open the camera parameters Params = camera. getparameters (); Params. setpreviewsize (display. getwidth (), display. getheight (); // sets the size of the previewed photo Params. setpreviewframerate (5); // number of photos captured by the camera per second. this is the Params of 5 images per second. setpictureformat (pixelformat. JPEG); // set the photo output format Params. set ("Jpeg-quality", 85); // sets the photo quality Params. setpicturesize (display. getwidth (), display. getheight (); // set the Photo size. Try {Camera. setparam Eters (Params); // sets the Camera Parameter camera. setpreviewdisplay (holder); // sets the surfaceviewcamera used by the camera. startpreview (); // enable the camera to preview ispriview = true;} catch (exception e) {e. printstacktrace () ;}@ overridepublic void surfacedestroyed (surfaceholder holder) {If (camera! = NULL) {If (ispriview) {// if you are previewing camera. stoppreview (); // stop preview ispriview = false;} camera. release (); // release the camera }}@ overridepublic Boolean onkeydown (INT keycode, keyevent event) {// override the method of activity if (camera! = NULL & event. getrepeatcount () = 0) {// event. the number of times the getrepeatcount () button is repeated. repeated times = 0, indicating that only one switch (keycode) {// case keyevent is clicked. keycode_search: // Search button // camera. autofocus (null); // auto focus. this is the auto focus callback method. it can be null // return true; Case keyevent. keycode_camera: // The photo key case keyevent. keycode_search: // search key. press these two keys to take a photo of camera. takepicture (null, new picturecallbackimpl (); // take a photo. call the takepicturecallback callback function to process the photo. Return true;} return Super. onkeydown (keycode, event);} class picturecallbackimpl implements picturecallback {// callback processing after photographing @ overridepublic void onpicturetaken (byte [] data, camera) {Bitmap bitmap = bitmapfactory. decodebytearray (data, 0, Data. length); file = new file (environment. getexternalstoragedirectory (), system. currenttimemillis () + ". jpg "); try {fileoutputstream outputstream = new fileoutputstream (File); bitmap. compress (compressformat. JPEG, 100, outputstream); // 100 is a lossless compression outputstream. close (); camera. startpreview (); // after taking the photo, the camera starts previewing again. otherwise, the screen does not move.} catch (exception e) {e. printstacktrace ();}}}}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.