Android WebView Long Press to identify the image, using zxing to identify whether the image is a two-dimensional code

Source: Internet
Author: User
Tags event listener

The main implementation of the Webwiew Onlongclick () method, using

Hittestresult htr = Gethittestresult (); // get the clicked content
Judging whether the click is a picture, its main code is:
    
 @Overridepublic boolean Onlongclick (View v) {//Long press Event Listener (Note: You need to implement the Longclickcallback interface and pass in the object) final Hittestresult htr = Gethittestresult ();//Gets the clicked content if (htr.gettype () = = hittestresult.image_type| | htr.gettype () = = hittestresult.image_anchor_type| | Htr.gettype () = = Hittestresult.src_image_anchor_type) {//Determine the type of the hit as Picture if (Mcallback!=null) { Mcallback.onlongclickcallback (Htr.getextra ());}} return false;} 
If you decide it is a picture, you can use zxing to identify whether it is a two-dimensional code image:
  @Override public    void Onlongclickcallback (final String imgurl) {        imgurl=imgurl;        Get to the picture address and do the appropriate processing for the        new Thread () {public            void run () {                decodeimage (imgurl);                Handler.sendemptymessage (0);}            ;        }. Start ();        ShowDialog ();    }


  First we convert the link address of the image to a picture:   
    
/** * Base address get network picture * @param surl Image Address * @return * @throws IOException */Public            Static Bitmap Getbitmap (String surl) {try {URL url = new URL (surl);            HttpURLConnection conn = (httpurlconnection) url.openconnection ();            Conn.setconnecttimeout (5000);            Conn.setrequestmethod ("GET");                if (conn.getresponsecode () = =) {InputStream InputStream = Conn.getinputstream ();                Bitmap Bitmap = Bitmapfactory.decodestream (InputStream);            return bitmap;        }} catch (Exception e) {e.printstacktrace ();    } return null; 
after the link has been transferred out of the picture in the recognition image is a two-dimensional code map:
    
/** * determine if the QR code * @param surl Image Address * @return */Private Boolean decodeimage (Str        ing surl) {result = Decodeimage.handleqrcodeformbitmap (Getbitmap (sURL));        if (result = = null) {ISQR = false;        }else {ISQR = true;    } return ISQR; 

If a picture of a two-dimensional code is sent handle to update adapter to display the QR code in the identification chart
@SuppressLint ("Handlerleak")    private Handler Handler = new Handler () {public        void Handlemessage (Message msg) { C4/>if (Msg.what = = 0) {                if (ISQR) {                    Adapter.add ("Two-dimensional code in the identification chart");                }                Adapter.notifydatasetchanged ();            }        };    };
If you click Save picture, use Asnytask thread to save the image to Local:
 /*** * Function: Save picture with thread * * @author WANGYP * */Private class SaveImage extends Asynctask<string, Voi             D, string> {@Override protected string Doinbackground (String ... params) {string result = "";                try {String sdcard;                if (fileutils.sdisexists ()) {sdcard = Environment.getexternalstoragedirectory (). GetAbsolutePath ();                }else{Sdcard=getfilesdir (). GetAbsolutePath ();                } File File = new file (sdcard + "/download");                if (!file.exists ()) {file.mkdirs ();                } file = new file (sdcard + "/download/" + New Date (). GetTime () + ". jpg");                InputStream inputstream = null;                URL url = new URL (imgurl);                HttpURLConnection conn = (httpurlconnection) url.openconnection ();                Conn.setrequestmethod ("GET"); Conn.setconnEcttimeout (20000);                if (conn.getresponsecode () = =) {InputStream = Conn.getinputstream ();                } byte[] buffer = new byte[4096];                int len = 0;                FileOutputStream OutStream = new FileOutputStream (file);                while (len = inputstream.read (buffer))! =-1) {outstream.write (buffer, 0, Len);                } outstream.close ();            result = "Picture saved to:" + File.getabsolutepath (); } catch (Exception e) {result = "Save failed!            "+ e.getlocalizedmessage ();        } return result; } @Override protected void OnPostExecute (String result) {Toast.maketext (h5activity.this,result,to Ast.        Length_short). Show (); }    }

If the user clicks on the identification QR code, the identified QR code link opens the browser:
Intent Intent = new Intent (Intent.action_view); Intent.setdata (Uri.parse (result.tostring ())); StartActivity (Intent); Closedialog ();








Android WebView Long Press to identify the image, using zxing to identify whether the image is a two-dimensional code

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.