The Android development Framework customizes the zxing two-dimensional code scanning interface and solves frame stretching problems _android

Source: Internet
Author: User
Tags event listener

First to show you the effect of the picture:

The scan is the following one, and the two-dimensional code is generated using the Zxing library.

Because of the change of several classes, or last year's things are almost forgotten, so only on the code of this class, the main is to change the Captureactivity.java

Package com.zxing.activity;
Import java.io.IOException;
Import Java.util.Vector;
Import android.app.Activity;
Import android.content.Intent;
Import Android.content.res.AssetFileDescriptor;
Import Android.graphics.Bitmap;
Import Android.media.AudioManager;
Import Android.media.MediaPlayer;
Import Android.media.MediaPlayer.OnCompletionListener;
Import Android.os.Handler;
Import Android.os.Vibrator;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceHolder.Callback;
Import Android.view.SurfaceView;
Import Android.widget.Toast;
Import COM.ERICSSONLABS.R;
Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.Result;
Import Com.zxing.camera.CameraManager;
Import Com.zxing.decoding.CaptureActivityHandler;
Import Com.zxing.decoding.CaptureActivityHandler.DecodeCallback;
Import Com.zxing.decoding.InactivityTimer;
Import Com.zxing.view.ViewfinderView;
/** * Initial the camera * @author Ryan.tang * @modifier Lemon * @use extends captureactivity and invoke the Init method after the Setcontentview method
*/Public abstract class Captureactivity extends activity implements Callback, Decodecallback {//private static final Strin
G TAG = "captureactivity";
protected activity context;
protected Surfaceview Surfaceview;
protected Viewfinderview Viewfinderview; /** initialization, must be after Setcontentview * @param context * @param viewfinderview/protected void init (activity context, Surfaceview s Urfaceview, Viewfinderview viewfinderview) {this.context = context; this.surfaceview = Surfaceview; this.viewfinderView
= Viewfinderview;
Cameramanager.init (Getapplication ());
Hassurface = false;
Inactivitytimer = new Inactivitytimer (this);
Private Captureactivityhandler handler;
Private Boolean hassurface;
Private vector<barcodeformat> decodeformats;
Private String CharacterSet;
Private Inactivitytimer Inactivitytimer;
Private MediaPlayer MediaPlayer;
Private Boolean playbeep;
Private static final Float Beep_volume =. F;
private Boolean vibrate;
@Override protected void Onresume () {super.onresume (); SurfaceHolder Surfaceholder = Surfaceview.getholder (); if (hassurface) {Initcamera (surfaceholder);} else {surfaceholder.addcallback (this); Surfaceholder.settype (
Surfaceholder.surface_type_push_buffers);
} decodeformats = null;
CharacterSet = null;
Playbeep = true;
Audiomanager Audioservice = (audiomanager) getsystemservice (Audio_service);
if (Audioservice.getringermode ()!= audiomanager.ringer_mode_normal) {playbeep = false;} initbeepsound ();
vibrate = true; @Override protected void OnPause () {super.onpause (); if (handler!= null) {handler.quitsynchronously (); handler = null
;
} cameramanager.get (). Closedriver (); @Override protected void OnDestroy () {inactivitytimer.shutdown (); Super.ondestroy ();} public static final String Resul
t_qrcode_string = "result_qrcode_string"; /** * Handler Scan result * @param result * @param barcode/public void Handledecode (result result, Bitmap barcode) {in
Activitytimer.onactivity ();
Playbeepsoundandvibrate (); String resultstring = Result.gettext (); Fixme if (Resultstring.equals ("")) {Toast.maketext (captureactivity.this, Scan failed!, Toast.length_short). Show (
);
} setresult (RESULT_OK, New Intent (). Putextra (result_qrcode_string, resultstring));
Finish (); private void Initcamera (Surfaceholder surfaceholder) {try {cameramanager.get (). Opendriver (Surfaceholder);} catch (IO Exception IoE) {return.} catch (RuntimeException e) {return;} if (handler = = null) {handler = new Captureactivityhand
Ler (This, decodeformats, CharacterSet, Viewfinderview, this); 
@Override public void Drawviewfinder () {viewfinderview.drawviewfinder (); @Override public void surfacechanged (surfaceholder holder, int format, int width, int height) {} @Override public void  Surfacecreated (Surfaceholder holder) {if (!hassurface) {hassurface = true; Initcamera (holder);}} @Override public void Surfacedestroyed (Surfaceholder holder) {hassurface = false;} public Handler GetHandler () {return Handler;} private VO ID Initbeepsound () {if (playbeep&& MediaPlayer = null) {//the volume on Stream_system isn't adjustable, and users found it//too loud,//So
We are on the music stream.
Setvolumecontrolstream (Audiomanager.stream_music);
MediaPlayer = new MediaPlayer ();
Mediaplayer.setaudiostreamtype (Audiomanager.stream_music);
Mediaplayer.setoncompletionlistener (Beeplistener);
Assetfiledescriptor file = Getresources (). OPENRAWRESOURCEFD (R.raw.beep);
try {Mediaplayer.setdatasource (File.getfiledescriptor (), File.getstartoffset (), file.getlength ()); File.close ();
Mediaplayer.setvolume (Beep_volume, Beep_volume);
Mediaplayer.prepare ();
catch (IOException e) {mediaPlayer = null;}}
Private static final long vibrate_duration = L; private void Playbeepsoundandvibrate () {if (playbeep && mediaPlayer!= null) {Mediaplayer.start ();} if (vibrate
{Vibrator Vibrator = (Vibrator) getsystemservice (Vibrator_service); vibrator.vibrate (vibrate_duration);}} /** * Beep has finished playing, rewind to queueUp another one. * Private final Oncompletionlistener Beeplistener = new Oncompletionlistener () {public void oncompletion (MediaPlayer med
Iaplayer) {Mediaplayer.seekto ();}};  }

How to: Create a new activity to inherit captureactivity and invoke the Init method after the Setcontentview method.
Example:

Camerascanactivity.java

Package zuo.biao.activity;
Import ZUO.BIAO.R;
Import Zuo.biao.library.interfaces.OnBottomDragListener;
Import Zuo.biao.util.ActivityUtil;
Import Android.content.Context;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.SurfaceHolder.Callback;
Import Android.view.SurfaceView;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import com.zxing.activity.CaptureActivity;
Import Com.zxing.camera.CameraManager;
Import Com.zxing.view.ViewfinderView; /** Scan Two-dimensional code activity * @author Lemon * @use Reference zuo.biao.library.ModelActivity * * public class Camerascanactivity extends Captur Eactivity implements Callback, Onclicklistener, Onbottomdraglistener {public static final String TAG = "Camerascanactivit
Y "; Startup Method <<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<</** Launches Intent * @param context * @param title * @return/public static Intent Createintent (Context context)
{Return to New Intent (context, camerascanactivity.class);} Startup Method >>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>> @Override protected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.camera_scan_activity); Init (this
, (Surfaceview) Findviewbyid (R.id.svcamerascan), (Viewfinderview) Findviewbyid (R.id.vfvcamerascan)); Functional collation Partitioning method, you must call <<<<<<<<<&Lt
Initview ();
InitData ();
Initlistener (); Functional collation Partitioning method, must invoke >>>>>>>>>>}//ui display area (Operation UI, but no data acquisition or processing code, no event listener code) <<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<< public void Initview () {//Must call}//ui display area (Operation UI, but no data acquisition or processing code , there is no event listener code) >>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>//data Data area (there is data acquisition or processing code, but no event listener code) <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<< public void InitData () {//Must call}//data data area (there is data acquiring or processing code but no event listener code) >>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>//listener Event Listening area (as long as there is an event listener code) <<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<< public void Initlistener () {//Must call Findviewbyid (
R.id.tvcamerascanreturn). Setonclicklistener (this);
Findviewbyid (R.id.ivcamerascanreturn). Setonclicklistener (this); FindviewbYid (r.id.ivcamerascanlight). Setonclicklistener (this);
Findviewbyid (R.id.ivcamerascanmyqrcode). Setonclicklistener (this); }//System self-monitoring method <<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<< @Override public void Ondragbottom (Boolean RightToLeft) {if (RightToLeft) {return;} finish (): @Override public void OnClick (View v) {SWITC H (V.getid ()) {case R.id.tvcamerascanreturn:case R.id.ivcamerascanreturn:ondragbottom (false);
R.id.ivcamerascanlight:switchlight (! IsOpen);
Break
Case R.id.ivcamerascanmyqrcode://break;
Default:break;
} private Boolean isOpen = false; /** turn on or off flashing lights * @param open/private void Switchlight (Boolean open) {if (open = = IsOpen) {return;} IsOpen = Cameramanag
Er.get (). Switchlight (open); }//class related monitoring <<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<//class related monitoring >>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>//System self-band monitoring method >>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>//listener Event Listening area (as long as there is an event listener code) >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>/internal class, as little as possible <<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<</internal class, as little as possible >>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>  >>>>>>>>>>>>>>>>>}

camerascanactivity Layout file Camera_scan_activity.xml

<?xml version= "." Encoding= "utf-"?> <framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "style=" @style/activity_page "> <surfaceview android:id=" @+id/svcamerascan "android:layout_width=" Wrap_content "android:layout_height=" wrap_content android:layout_gravity= "center"/> <!--must be at the lowest level and cannot be specified as wide, Otherwise, scanning reading is very difficult to achieve--> <com.zxing.view.viewfinderview android:id= "@+id/vfvcamerascan android:layout_width=" Match_ Parent "android:layout_height=" match_parent "/> <linearlayout style=" @style/ll_vertical_match_match "Android: Baselinealigned= "false" > <relativelayout style= "@style/topbar_bg" android:background= "@color/white_alpha" > <textview android:id= "@+id/tvcamerascanreturn" style= "@style/topbar_left_btn" android:text= ""/> < TextView style= "@style/topbar_title" android:layout_centerhorizontal= "true" android:text= "sweep a sweep"/> </ relativelayout> <linearlayout style= "@style/ll_vertical_match_match" android:layout_gravity= "Center_hoRizontal "android:layout_weight=" > <textview style= "@style/text_middle_white" android:layout_margin= "DP" android:text= "@string/camera_scan_remind"/> </LinearLayout> <linearlayout style= "@style/ll_horizontal
_match_wrap "android:layout_gravity=" Bottom "android:background=" @color/white_alpha "android:gravity=" Center ">
<linearlayout style= "@style/ll_vertical_wrap_wrap" android:layout_margin= "DP" android:paddingbottom= "DP" Android:paddingleft= "DP" android:paddingright= "DP" android:paddingtop= "DP" > <imageview android:id= "@+id/" Ivcamerascanreturn "style=" @style/wrap_wrap "android:background=" @drawable/cilcle_gray_to_white "android:padding= "DP" android:src= "@drawable/back_black_light"/> <textview style= "@style/text_small" android:layout_margintop = "DP" android:text= "returns"/> </LinearLayout> <linearlayout style= "@style/ll_vertical_wrap_wrap" Android:
Layout_margin= "DP" android:paddingbottom= "DP" android:paddingleft= "DP" android:paddingright= "DP"Android:paddingtop= "DP" > <imageview android:id= "@+id/ivcamerascanlight" style= "@style/wrap_wrap" Android: background= "@drawable/cilcle_gray_to_white" android:padding= "DP" android:src= "@drawable/flash_light"/> <
TextView style= "@style/text_small" android:layout_margintop= "DP" android:text= "Turn on lights/Turn off lights"/> </LinearLayout>
<linearlayout style= "@style/ll_vertical_wrap_wrap" android:layout_margin= "DP" android:paddingbottom= "DP" Android:paddingleft= "DP" android:paddingright= "DP" android:paddingtop= "DP" > <imageview android:id= "@+id/" Ivcamerascanmyqrcode "style=" @style/wrap_wrap "android:background=" @drawable/cilcle_gray_to_white "Android: Padding= "DP" android:src= "@drawable/qrcode"/> <textview style= "@style/text_small" android:layout_margintop= "  DP "android:text= my business Card"/> </LinearLayout> </LinearLayout> </LinearLayout> </FrameLayout>

Layout file because of the use of some of the style,color in Zblibrary, only this layout will be a number of errors. You can create a new layout file and put the Viewfinderview in the outermost layout file. Of course, download the following attached to the source code is not the problem.

By the right, remember to add these permissions to the Androidmanifest.xml:

<uses-permission android:name= "Android.permission.CAMERA"/>
<uses-feature android:name= " Android.hardware.camera "/>
<uses-feature android:name=" Android.hardware.camera.autofocus "/>"
<uses-permission android:name= "Android.permission.FLASHLIGHT"/>

Attached source code (including the use of methods)

GitHub Source Https://github.com/TommyLemon/Android-ZBLibrary

Open source China source code http://git.oschina.net/Lemon19950301/Android-ZBLibrary

About the Android development framework of the custom zxing two-dimensional code scanning interface and solve the frame stretching problem for you to introduce here, the above content is a bit long, I hope we can patiently read, have any questions welcome to my message, small series will promptly reply to everyone, here also thank you for the cloud Habitat Community website support!

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.