Zxing source code analysis (1) camera, zxingcamera

Source: Internet
Author: User

Zxing source code analysis (1) camera, zxingcamera

First, let's download the source code of zxing. There are a lot of resources on the Internet. I downloaded 2.3 of them. I have to say that the package provided by Google contains a wide range of functions.

After I decompress the downloaded package, find the android folder and import it to ecplise. Let's analyze the architecture 1 and book. If the query result is book information, you can choose to query the further details of the book. This package contains related categories for searching and displaying books. Ii. camera/camera. open is a class about cameras. The core class is CameraManager 3. clipboard 4. encode: a set of components for the encoding function. The core class is QRCodeEncoder, the final encoding is MultiFormatWriter Class 5. history: Scan history Management. The core class is HistoryManager. 6. result: The barcode scan results are divided into different types, all types, all are defined in com. google. zxing. client. result. in ParsedResultType, there are corresponding processing methods for different types; xxxResultHandler, all ResultHandler are included in this package. Different xxxResultHandler also provides several buttons to be displayed on the scan result page. The text of each button and events to be bound are analyzed from the source code of the camera, because I only use the scan and output results sections, the other sections will remove the camera section from a total of six categories: OpenCameraInterface: open camera class CameraConfigurationManager: Camera configuration class CameraManager: Core class, camera management class AutoFocusManager: No FrontLightMode: enumeration PreviewCallback: preview callback Class I. OpenCameraInterface. This is a class opened by a camera. The method inside is open (), that is, to open the camera, I commented out
Public static Camera open () {// obtain the number of cameras int numCameras = Camera. getNumberOfCameras (); // exit if (numCameras = 0) {Log if no camera is found. w (TAG, "No cameras! "); Return null;} int index = 0; while (index <numCameras) {// initialize the Camera Information Class Camera. cameraInfo cameraInfo = new Camera. cameraInfo (); // get the Camera information Camera. getCameraInfo (index, cameraInfo); // determines whether the camera is a rear camera if (cameraInfo. facing = Camera. cameraInfo. CAMERA_FACING_BACK) {break;} index ++;} // turn on the Camera camera Camera; if (index <numCameras) {Log. I (TAG, "Opening camera #" + index); camera = Camera. open (index);} else {Log. I (TAG, "No camera facing back; returning camera #0"); camera = Camera. open (0);} return camera ;}

Enable the camera in this method

Ii. CameraConfigurationManager camera configuration class. In this class, we mainly configure parameters such as Preview resolution, Flash, and focus. The following methods mainly include initFromCameraParameters (). By calling the findBestPreviewSizeValue method, to get the best camera preview resolution
Void initFromCameraParameters (Camera camera) {// obtain the Camera Parameter Camera. parameters parameters = camera. getParameters (); WindowManager manager = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE); Display display = manager. getdefadisplay display (); // construct a Point theScreenResolution = new Point (); // assign values to the Point, and display the screen width and High Resolution. getSize (theScreenResolution); screenResolution = theScreenResolution; Log. I (TAG, "Screen resolution:" + screenResolution); cameraResolution = findBestPreviewSizeValue (parameters, screenResolution); Log. I (TAG, "Camera resolution:" + cameraResolution );}

 

SetDesiredCameraParameters () method, sets the findBestPreviewSizeValue () method of the camera's flash and focus parameters, removes the inappropriate resolution, and selects the best resolution. 3. CameraManager is the core class of the entire camera, other classes are called in this class, which is used to uniformly manage camera initialization and parameters. openDriver method ()
Public synchronized void openDriver (SurfaceHolder holder) throws IOException {Camera theCamera = camera; if (theCamera = null) {theCamera = OpenCameraInterface. open (); if (theCamera = null) {throw new IOException ();} camera = theCamera;} // sets the camera preview function theCamera. setPreviewDisplay (holder); // if (! Initialized) {initialized = true; // initialize the camera parameters and select the best preview resolution configManager. callback (theCamera); if (requestedFramingRectWidth> 0 & amp; timeout> 0) {setManualFramingRect (response, response); requestedFramingRectWidth = 0; callback = 0 ;}} Camera. parameters parameters = theCamera. getParameters (); String parametersFla Ttened = parameters = null? Null: parameters. flatten (); // Save these, temporarily try {// set necessary parameters, including focus, flashlight, and other configManager. setDesiredCameraParameters (theCamera, false);} catch (RuntimeException re) {// Driver failed Log. w (TAG, "Camera rejected parameters. setting only minimal safe-mode parameters "); Log. I (TAG, "Resetting to saved camera params:" + parametersFlattened); // Reset: if (parametersFlattened! = Null) {parameters = theCamera. getParameters (); parameters. unflatten (parametersFlattened); try {theCamera. setParameters (parameters); configManager. setDesiredCameraParameters (theCamera, true);} catch (RuntimeException re2) {// Well, darn. give up Log. w (TAG, "Camera rejected even safe-mode parameters! No configuration ");}}}}

 

These are the core methods of Camera. We can see that the program runs by calling CaptureActivity, and some methods of Camera are called in CaptureActivity, in CaptureActivity, use the initCamera (surfaceHolder); Method to start scanning and finally obtain the scan result. In the layout file
<SurfaceView android:id="@+id/preview_view"               android:layout_width="fill_parent"               android:layout_height="fill_parent"/>   <com.google.zxing.client.android.ViewfinderView      android:id="@+id/viewfinder_view"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:visibility="gone" />

 

One is a preview window, and the other is a scan window that is displayed with a custom display. At this point, I have summarized it and basically understood how it runs and the role of Camera's core methods.

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.