Introduction to two-dimensional Code fast scanning optimization scheme (i.)--How to automatically fill the light when the light is low.

Source: Internet
Author: User

QR Code Scan Code is a very common technology, there are many open source projects can be implemented, such as Zxing project. Https://github.com/zxing

Here is not the focus of the introduction of zxing in the implementation of the QR Code scan code, but to introduce, on the Fast Scan code technology is how to optimize the experience.

To achieve in the light, the phone automatically fill the light, actually very simple, everyone knows that there are many sensors on the phone, one of the most important sensor is the light sensor.

This is usually used when you take pictures. So how to apply this to the QR code of the sweep code experience up.

We can use the cell phone's light sensor for external light sensing, so when the light is dark, we can automatically fill the light. Here we take the example of Android phone, to explain

On an Android phone, we define a button and a textview,textview to display the intensity index of light received from the light sensor. Button buttons are used when the light is weak to automatically come out of this button, you can click on the phone to open the flash. When the light is strong, the button is automatically hidden.

<?xml version= "1.0" encoding= "Utf-8"?>
<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
.......
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Fill Light"
Android:id= "@+id/flashlightbutton"
Android:layout_centervertical= "true"
Android:layout_centerhorizontal= "true"/>

<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/ligthsensorview"
android:layout_below= "@+id/flashlightbutton"
Android:layout_centerhorizontal= "true"
android:layout_margintop= "92DP"/>
</RelativeLayout>
</FrameLayout>
In the activity, we define the buttons and TextView above
Fill Light button
Private Button Flashlightbutton;
Shows the current light intensity
Private TextView Ligthsensorview;
Light sensor related
Private Sensormanager SM;
Private Sensor ligthsensor;
Flashlightbutton  = (Button) Findviewbyid (R.id.flashlightbutton);
Ligthsensorview = (TextView) Findviewbyid (R.id.ligthsensorview);
Light sensor Use Registration
SM = (Sensormanager) getsystemservice (Sensor_service);
Ligthsensor = Sm.getdefaultsensor (sensor.type_light);
Sm.registerlistener (New Mysensorlistener (), Ligthsensor, sensormanager.sensor_delay_normal);
Set Button Listener
Flashlightbutton.setonclicklistener (New Flashlightlistener ());

Monitoring implementation of Flashlightbutton
Class Flashlightlistener implements view.onclicklistener{
Boolean isopen = false;
@Override
public void OnClick (view view) {
if (isopen) {
Turn Off the lights
Cameramanager.get (). Closeflashlight ();
IsOpen = false;
}
else{
Turn on the light
Cameramanager.get (). Openflashlight ();
IsOpen = true;
}
}
}
Sensor monitoring
public class Mysensorlistener implements Sensoreventlistener {

public void onaccuracychanged (sensor sensor, int accuracy) {

}

Get the intensity of the light
public void Onsensorchanged (Sensorevent event) {
Get precision
float acc = event.accuracy;
Get light intensity
Float lux = event.values[0];
SB = "Accuracy:" +acc+ ", light intensity:" +lux+ "camera mode:";
Light intensity displayed on the Ligthsensorview

Ligthsensorview.settext (SB);
int retval = Float.compare (Lux, (Float) 10.0);
if (retval>0) {
Light Intensity >10.0 Hide button
Flashlightbutton.setvisibility (view.invisible);
}
else {
Show button
Flashlightbutton.setvisibility (view.visible);
}
}
}
//Turn on the flash by camera 
public static void Turnlightoncamera (camera mcamera) {
Mcamera.startpreview ();
Camera.parameters Parameters = Mcamera.getparameters ();
List<string> flashmodes = Parameters.getsupportedflashmodes ();
String Flashmode = Parameters.getflashmode ();
if (! Camera.Parameters.FLASH_MODE_TORCH.equals (Flashmode)) {
//turn on Flash
if (Flashmodes.contains (Camera.para Meters. Flash_mode_torch) {
Parameters.setflashmode (Camera.Parameters.FLASH_MODE_TORCH);
Mcamera.setparameters (parameters);
Mcamera.startpreview ();
Isopenflash = true;
}
}
}
Turn off the flash by camera
public static void Turnlightoffcamera (Camera mcamera) {
Mcamera.stoppreview ();
Camera.parameters Parameters = Mcamera.getparameters ();
list<string> flashmodes = Parameters.getsupportedflashmodes ();
String Flashmode = Parameters.getflashmode ();
if (! Camera.Parameters.FLASH_MODE_OFF.equals (Flashmode)) {
Turn off the Flash
if (Flashmodes.contains (Camera.Parameters.FLASH_MODE_OFF)) {
Parameters.setflashmode (Camera.Parameters.FLASH_MODE_OFF);
Mcamera.setparameters (parameters);
}
}
Isopenflash = false;
}
Complete the above optimization, then we sweep the code will have a further experience optimization, depending on the intensity of the light to determine whether the need to scan code, to fill the light.


Introduction to two-dimensional Code fast scanning optimization scheme (i.)--How to automatically fill the light when the light is low.

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.