Camera, a multimedia application in Android

Source: Internet
Author: User

Mainactivity. Java

 

Code

  Package  Com. Amaker. ch11.app;

Import Java. Io. file;
Import Java. Io. fileoutputstream;
Import Java. Io. ioexception;

Import Android. App. activity;
Import Android. Graphics. pixelformat;
Import Android. Hardware. camera;
Import Android. OS. asynctask;
Import Android. OS. Bundle;
Import Android. OS. environment;
Import Android. View. keyevent;
Import Android. View. surfaceholder;
Import Android. View. surfaceview;

Public Class Mainactivity Extends Activity {
// Declare the camera interface component surfaceview
Private Surfaceview;
// Declare the interface control component surfaceholder
Private Surfaceholder;
// Declare camera
Private Camera camera;

@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Instantiate the photo Interface Component
Surfaceview = (Surfaceview) findviewbyid (R. Id. Preview );
// Obtain surfaceholder from surfaceview
Surfaceholder = Surfaceview. getholder ();
// Add callback for surfaceholder
Surfaceholder. addcallback (surfacecallback );
Surfaceholder. settype (surfaceholder. surface_type_push_buffers );
}

// Responds to button events
@ Override
Public Boolean Onkeydown ( Int Keycode, keyevent event ){
If (Keycode = Keyevent. keycode_camera | Keycode = Keyevent. keycode_search ){
Takepic ();
Return True ;
}
Return Super . Onkeydown (keycode, event );
}
// Photo Taking Method
Private Void Takepic (){
// Stop Preview
Camera. stoppreview ();
// Photograph
Camera. takepicture ( Null , Null , Picturecallback );
}
// Photo callback
Camera. picturecallback = New Camera. picturecallback (){
@ Override
Public Void Onpicturetaken ( Byte [] Data, camera ){
New Savepicturetask(cmd.exe cute (data );
Camera. startpreview ();
}
};

// Save photo task
Class Savepicturetask Extends Asynctask < Byte [], String, string > {
@ Override
Protected String doinbackground ( Byte []... Params ){
// Create a file
File picture = New File (environment. getexternalstoragedirectory (), " Picture.jpg " );
// If the file exists, delete it.
If (Picture. exists () picture. Delete ();
Try {
// Get file output stream
Fileoutputstream fos = New Fileoutputstream (picture. getpath ());
// Write to this file
FOS. Write (Params [ 0 ]);
// Close file stream
FOS. Close ();
} Catch (Exception e ){
E. printstacktrace ();
}
Return Null ;
}
}
// Surfacehodler callback to handle camera opening, camera closing, and Photo size changes
Surfaceholder. Callback surfacecallback = New Surfaceholder. Callback (){
@ Override
Public Void Surfacecreated (surfaceholder holder ){
// Turn on the camera
Camera = Camera. open ();
Try {
// Set Preview
Camera. setpreviewdisplay (holder );
} Catch (Ioexception e ){
E. printstacktrace ();
}
}

@ Override
Public Void Surfacechanged (surfaceholder holder, Int Format, Int Width,
Int Height ){
// Obtain camera parameters
Camera. parameters = Camera. getparameters ();
// Set the Photo size
Parameters. setpreviewsize (width, height );
// Set the photo format
Parameters. setpictureformat (pixelformat. JPEG );
// Set camera parameters
Camera. setparameters (parameters );
// Start Preview
Camera. startpreview ();
}

@ Override
Public Void Surfacedestroyed (surfaceholder holder ){
// Stop Preview
Camera. stoppreview ();
// Release camera Resources
Camera. Release ();
Camera = Null ;
}
};
}

 

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Android. View. surfaceview xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/preview"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
</Android. View. surfaceview>

 

Androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. Amaker. ch11.app"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = ". mainactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

</Application>
<Uses-SDK Android: minsdkversion = "3"/>

</Manifest>

Related Article

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.