Created
October 2, 2017 23:37
-
-
Save rajdeol/5f37ae399a435ee5d405dc52572ef5b6 to your computer and use it in GitHub Desktop.
In response to the comment http://blog.whomeninja.in/android-barcode-scanner-vertical-orientation-and-camera-flash/#comment-745 Android QR-Code Scanner After scanning a QR-Code check and verify if it is a HTTP URL and open it in a webview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package in.whomeninja.android_barcode_scanner; | |
import android.content.Intent; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.google.zxing.integration.android.IntentIntegrator; | |
import com.google.zxing.integration.android.IntentResult; | |
public class HomeActivity extends ActionBarActivity { | |
private String codeFormat,codeContent; | |
private TextView formatTxt, contentTxt; | |
private WebView main_web_view; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_home); | |
formatTxt = (TextView)findViewById(R.id.scan_format); | |
contentTxt = (TextView)findViewById(R.id.scan_content); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_home, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
/** | |
* event handler for scan button | |
* @param view view of the activity | |
*/ | |
public void scanNow(View view){ | |
IntentIntegrator integrator = new IntentIntegrator(this); | |
// set to scan QR-Code | |
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES); | |
integrator.setPrompt(this.getString(R.string.scan_bar_code)); | |
integrator.setResultDisplayDuration(0); | |
integrator.setWide(); // Wide scanning rectangle, may work better for 1D barcodes | |
integrator.setCameraId(0); // Use a specific camera of the device | |
integrator.initiateScan(); | |
} | |
/** | |
* function handle scan result | |
* @param requestCode scanned code | |
* @param resultCode result of scanned code | |
* @param intent intent | |
*/ | |
public void onActivityResult(int requestCode, int resultCode, Intent intent) { | |
//retrieve scan result | |
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); | |
if (scanningResult != null) { | |
//we have a result | |
String scanContent = scanningResult.getContents(); | |
String scanFormat = scanningResult.getFormatName(); | |
// process received data | |
if(scanContent != null && !scanContent.isEmpty()){ | |
processScannedData(scanContent); | |
}else{ | |
Toast toast = Toast.makeText(getApplicationContext(),"Scan Cancelled", Toast.LENGTH_SHORT); | |
toast.show(); | |
} | |
}else{ | |
Toast toast = Toast.makeText(getApplicationContext(),"No scan data received!", Toast.LENGTH_SHORT); | |
toast.show(); | |
} | |
} | |
/** | |
* process xml string received from aadhaar card QR code | |
* @param scanData | |
*/ | |
protected void processScannedData(String scanData){ | |
Log.d("Rajdeol",scanData); | |
// test if string is a valid URL | |
if(Patterns.WEB_URL.matcher(scanData).matches()){ | |
// open in a webview, this webview should be added in your layout | |
main_web_view = (WebView) findViewById(R.id.mainWebView); | |
//enable javascript | |
main_web_view.getSettings().setJavaScriptEnabled(true); | |
// load URL | |
main_web_view.loadUrl(scanData); | |
}else{ | |
// error | |
Toast toast = Toast.makeText(getApplicationContext(),"Scanned String is not valid URL", Toast.LENGTH_SHORT); | |
toast.show(); | |
} | |
}// EO function | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment