Viewfinderview customizes the view and implements a simple scanning interface. This article record my understanding of Android camera in the code process. Since I started to write a technical blog, there are many shortcomings in the first two articles.
I wrote them as needed. It is estimated that it is difficult for everyone to have a clear understanding of what I wrote. This article tries to change the style and try to express my understanding, so that everyone can understand it.
Before reading the camera code in Barcode development, let's give a brief introduction to Android camera development.
The first is the permissions required to use camera.
1 <uses-permission android:name="android.permission.CAMERA"/>
2 <uses-feature android:name="android.hardware.camera"/>
The following is a simple example of camera. It is as simple as simply taking the scene, that is, opening the camera and displaying the scene on the screen.
1 import java. Io. ioexception;
2 Import Android. App. activity;
3 Import Android. Hardware. camera;
4 Import Android. OS. Bundle;
5 import Android. View. surfaceholder;
6 Import Android. View. surfaceview;
7
8 public class cameratestactivity extends activity implements surfaceholder. Callback {
9 private surfaceholder;
10 private camera;
11/** called when the activity is first created .*/
12 @ override
13 public void oncreate (bundle savedinstancestate ){
14 super. oncreate (savedinstancestate );
15 setcontentview (R. layout. Main );
16 surfaceview = (surfaceview) findviewbyid (R. Id. preview_view );
17 surfaceholder = surfaceview. getholder ();
18 surfaceholder. addcallback (this );
19 surfaceholder. settype (surfaceholder. surface_type_push_buffers );
20}
21 @ override
22 public void surfacechanged (surfaceholder arg0, int arg1, int arg2, int arg3 ){
23 // todo auto-generated method stub
24}
25 @ override
26 Public void surfacecreated (surfaceholder arg0 ){
27 // todo auto-generated method stub
28 camera = camera. open ();
29
30 camera. parameters = camera. getparameters ();
31 parameters. setpreviewsize (480,320); // set
32 camera. setparameters (parameters );
33 try {
34 camera. setpreviewdisplay (surfaceholder );
35} catch (ioexception e ){
36 system. Out. println (E. getmessage ());
37}
38 camera. startpreview ();
39}
40 @ override
41 public void surfacedestroyed (surfaceholder arg0 ){
42 // todo auto-generated method stub
43 If (camera! = NULL ){
44 camera. stoppreview ();
45}
46 camera. Release ();
47 camera = NULL;
48}
49}
The R. Id. preview_view is as follows:
1 <SurfaceView
2 android:id="@+id/preview_view"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent" />
First, this activity implements the surfaceholder. Callback interface and overrides the three methods of this interface.
For more information about surfaceholder, surfaceview, and surfaceholder. Callback, see here for details.
Http://www.cnblogs.com/bausch/archive/2011/10/20/2219068.html
Surfaceview can obtain and display the data captured by the camera hardware. In the code above, the surfaceholder object is initialized first. And the surfacecreated function is rewritten. In this function, the basic operations for camera to open the scene are completed. First, it is camera. open () to get a camera object, and initialize some camera parameters, such as format, image preview size, and refresh rate. After setting the Preview display, do not forget startpreview. Because the camera's landscape needs to be set to a portrait screen for the initial development project, the barcode scheme is set to a landscape screen. When you try to adjust the image display direction, I thought it was re-set in manifest,
1 android:screenOrientation="landscape"
If you set landscape to portrait, the result is quite unexpected. The screen is displayed in a vertical way, but the content after the scene retrieval is opposite to the display. The mobile phone uses a landscape while the screen is displayed in a horizontal manner. You cannot simply adjust the value of this parameter to change the direction. Call the following function to reset the display direction of the preview image.
1 camera.setDisplayOrientation(90);
After the display direction is adjusted, the scenario is finally normal. However, when previewing the photo results, we found that this was an illusion. The camera's underlying landscape was still horizontal, but the orientation was adjusted during the preview. This also caused the problem of displaying the photo stretch. This is not detailed.
I can read this article. The summary of Android camera is more comprehensive and practical than mine.
Http://www.diybl.com/course/3_program/java/android/20111201/563696.html