i wants to automatically navigate to the site after scanning qr code,hear is my code which crashes after scanning qr code.
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void QrScanner(View view){
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView);
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
#Override
public void handleResult(Result rawResult) {
// Do something with the result here
Log.e("handler", rawResult.getText()); // Prints scan results
Log.e("handler", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode)
WebView webview = (WebView)this.findViewById(R.id.WebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadDataWithBaseURL("", rawResult.toString(), "text/html", "UTF-8", "");
}
}
Related
I would like redirect to web page after QR code scan (Android Studio Zxing Scanner), after scan code when I press buttontoast, it is redirect to an URL, but I want to redirect without button press automatically alter scan.
No idea where to place the URL for redirect:
mWebView.loadUrl("https://example.com/d2.aspx?name=" + MainActivity.resulttextview.getText());
Using (Webview)
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resulttextview = findViewById(R.id.barcodetextview);
scanbutton = findViewById(R.id.buttonscan);
buttontoast = findViewById(R.id.buttontoast);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
scanbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), ScanCodeActivity.class));
}
});
buttontoast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, resulttextview.getText(), Toast.LENGTH_SHORT).show();
mWebView.loadUrl("https://example.com/d2.aspx?name=" + MainActivity.resulttextview.getText());
}
});
ScanCodeActivity.java:
public class ScanCodeActivity extends Activity implements ZXingScannerView.ResultHandler {
int MY_PERMISSIONS_REQUEST_CAMERA=0;
ZXingScannerView scannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
}
#Override
public void handleResult(Result result) {
MainActivity.resulttextview.setText(result.getText());
onBackPressed();
}
#Override
protected void onPause() {
super.onPause();
scannerView.stopCamera();
}
#Override
protected void onPostResume() {
super.onPostResume();
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
scannerView.setResultHandler(this);
scannerView.startCamera();
}
}
enter code here
If you want to open the url directly when you scan a Qr code, add your logic in the handle result method like this:
#Override
public void handleResult(Result result) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://google.com"));
startActivity(intent)
}
This will open link in any browser installed on phone. If you want to specifically open a link in webview inside your application, call startActivityForResult in MainActivity, set result in ScanCodeActivity after you detect the URL and handle the sent result in OnActivityResult in MainActivity. I hope this might help you.
Hello I want to thank you in advance for your answers. My problem is I am using Zxing to scan qr codes. And I want to paste the scan value on the my edit text at Login Activity.
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.zxing.Result;
import com.logizard.logizard_go.R;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScanResult extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_result);
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView); // Set the scanner view as the content view
}
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
#Override
public void handleResult(Result rawResult) {
// Do something with the result here
// Log.v("tag", rawResult.getText()); // Prints scan results
// Log.v("tag", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
Login.editTextUser.setText(rawResult.getText());
onBackPressed();
// If you would like to resume scanning, call this method below:
//mScannerView.resumeCameraPreview(this);
}
}
This is the sample code that I use but every time I use the button for scan nothing happens.
It looks like that you have a Login(Activity) that open the ScanResult(Activity) and you want to return the value of the scanned to Login
I suggest to you to look this doc, but also maybe you could move the handlingscan part of your code to Login activity
... implements ZXingScannerView.ResultHandler{
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
#Override
public void handleResult(Result rawResult) {
editTextUser.setText(rawResult.getText());
}
}
BETTER do like that
in Login
static final int GET_SCANNED = 1;
...
private void openScanResult(){
Intent i = new Intent(this, ScanResult.class);
startActivityForResult(i, GET_SCANNED);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_SCANNED) {
if(resultCode == Activity.RESULT_OK){
String result = data.getStringExtra("scanned");
editTextUser.setText(result)
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
in ScanResult
#Override
public void handleResult(Result rawResult) {
Intent returnIntent = new Intent();
returnIntent.putExtra("scanned",rawResult.getText());
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
How to show bar-code scanning screen always in android.
IntentIntegrator integrator = new IntentIntegrator(activity);
first you need to add a FrameLayout to your layout.xml as a container for barcodeScannet :
<FrameLayout
android:id="#+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="350dp" />
after that you need to implement ZXingScannerView.ResultHandler in your activity or fragment
then you need to add scanner to this view
private ZXingScannerView mScannerView;
ViewGroup v = (ViewGroup) mainView.findViewById(R.id.barcode_scanner);
mScannerView = new ZXingScannerView(getActivity());
v.addView(mScannerView);
also you need to override onResume and onPause to start stop the camera:
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
// aslso check for camera permission here too
}
#Override
public void onPause() {
mScannerView.stopCamera(); // Stop camera on pause
super.onPause();
}
then :
#Override
public void handleResult(Result rawResult) {
AppLog.logE("result content", rawResult.getText()); // Prints scan results
AppLog.logE("result name", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
// the resault of barcode will be given as an string rawResult.getText()
// and you can do whatEver you want with it
// handleBarcodeResult(rawResult.getText());
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
mScannerView.resumeCameraPreview(BillPaymentFragment.this);
}
}, 2000);
}
and finally whenever you want to start the barcode scanner just call the following code :
mScannerView.startCamera();
I have come across this problem when scanning a barcode in my application, actually, the QR code is also present on some of the products beside barcode. so the zxing also scans QR code.
public class QRScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
// begin variable listing
private ZXingScannerView mScannerView;
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setFlash(true);
List<BarcodeFormat> myformat = new ArrayList<>();
myformat.add(BarcodeFormat.EAN_13);
myformat.add(BarcodeFormat.EAN_8);
myformat.add(BarcodeFormat.RSS_14);
myformat.add(BarcodeFormat.CODE_39);
myformat.add(BarcodeFormat.CODE_93);
myformat.add(BarcodeFormat.CODE_128);
myformat.add(BarcodeFormat.ITF);
myformat.add(BarcodeFormat.CODABAR);
myformat.add(BarcodeFormat.DATA_MATRIX);
myformat.add(BarcodeFormat.PDF_417);
mScannerView.setFormats(myformat);
// Programmatically initialize the scanner view
// Set the scanner view as the content view
}
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
#Override
public void handleResult(Result rawResult) {
// Do something with the result here
result=rawResult.getText();
Log.e("qr_Text1",result);
finish();
// Prints the scan format (qrcode, pdf417 etc.)
// If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this);
}
}
compile 'me.dm7.barcodescanner:zxing:1.8.4'
I am using this kind of text to speech in one of my class in my app(Code edited to show outlook & exact requirement.). I will show some content on my view & if we click the button , I want to play the sound that is by using this texttospeech engine... But for First time it is not playing the sound. From the next click onwards the TEXTTOSPEECH engine is working nicely
Iwant to know how to overcome this issue....
public class LearnActivity extends Activity implements OnClickListener, OnInitListener {
AudioManager audioManager;
float volume;
TextToSpeech textToSpeech;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learn);
textToSpeech = new TextToSpeech(this, this);
textToSpeech.setLanguage(Locale.US);
textToSpeech.setSpeechRate(0.95f);
method();
}
public void method(){
bt.setonClickListener(new onClickListener(){
public void onClick(View v){
playSound(datasource.getItemSound);
}
});
}
public void playSound(String sound){
textToSpeech.speak(sound,TextToSpeech.QUEUE_FLUSH,null);
}
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
}
NOTE:- This also meet my Requirement, How to play sound from TEXTTOSPEECH engine directly without using any onClicks etc.,... because I also wants to play a startup sound that too with Android's Text-To-Speech engine only...
That's because you are clicking the button before the engine is ready.
You have to check if the TTS engine has successfully initialized on your onInit() method and enable/disable the play button accordingly.
Assuming that bt in your code is some sort of View that has setEnabled(boolean) method:
#Override
public void onInit(int status) {
bt.setEnabled(status == TextToSpeech.SUCCESS);
}
You always have to assume that the engine has not been initialized and hence keep your play button disabled by default.
I used RXJava to make a class that solves this problem. If your TTS engine is not ready when you want to use the speak method it will wait for the engine to get ready and then speaks the given string.
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Pair;
import android.widget.Toast;
import java.util.Locale;
import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
public class Pronunciation {
private TextToSpeech textToSpeech;
private int languageResult;
private boolean noError;
private final String errorMessage="Something went wrong with your text to speech engine";
private PublishSubject<Boolean> engineIsReady=PublishSubject.create();
private PublishSubject<Pair<String,Integer>> speakObservable=PublishSubject.create();
private CompositeDisposable compositeDisposable=new CompositeDisposable();
public Pronunciation(Context context) {
textToSpeech=new TextToSpeech(context, status -> {
if (status!=TextToSpeech.ERROR){
languageResult= textToSpeech.setLanguage(Locale.ENGLISH);
engineIsReady.onNext(true);
} else {
Toast.makeText(context,errorMessage
,Toast.LENGTH_LONG).show();
}
});
if (languageResult==TextToSpeech.LANG_MISSING_DATA||languageResult== TextToSpeech.LANG_NOT_SUPPORTED){
noError =false;
Toast.makeText(context,errorMessage
,Toast.LENGTH_LONG).show();
}else { noError =true;}
compositeDisposable.add( Observable.combineLatest(speakObservable, engineIsReady,
(stringIntegerPair, aBoolean) -> stringIntegerPair)
.subscribeOn(Schedulers.io())
.subscribe(pair->{
if (noError)
textToSpeech.speak( (pair).first
,(pair).second,null,null);
}));
}
public void speak(String text,int queueMode){
speakObservable.onNext(new Pair<>(text,queueMode));
}
public void stop(){
if (textToSpeech!=null){
textToSpeech.stop();
textToSpeech.shutdown();
}
compositeDisposable.clear();
}
}
first add RxJava dependency in your Gradle file
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
Then create an instance of this class in the onCreate method of your activity or fragment.Now you can pass the string and queue mode to the speak method.
pronunciation.speak("Hi", TextToSpeech.QUEUE_FLUSH));
Don't forget to call the stop method in onDestroy or onDetach to avoid memory leak
#Override
public void onDetach() {
super.onDetach();
pronunciation.stop();
}
you should write your program ...
public class MainActivity extends Activity implements TextToSpeech.OnInitListener, OnUtteranceCompletedListener {
TextToSpeech t1;
protected void onCreate(Bundle savedInstanceState) {
t1=new TextToSpeech(MainActivity.this, MainActivity.this);
}/////on creat
protected void onDestroy() {
if(t1!=null) {
t1.stop();
t1.shutdown();
t1=null;
}
super.onDestroy();
}
public void onInit(int arg0) {
t1.setOnUtteranceCompletedListener(this);
}
}//mainactivity
Add this command when button is clicked or everywhere you want to speak text.
t1.speak(text, TextToSpeech.QUEUE_FLUSH, null);
Just Put Delay For 5 Second And It's Working without any button Click
public class Final_Text_To_Speech_Activity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private TextToSpeech tts; // For Text to Speech
CardView ScanProduct, SearchProduct;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
init();
// Just Put Delay For 5 Second And It's Working without any button Click
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
SpeakOutOnce("Welcome to Text To Speech Application");
}
}, 5000);
}
#Override
protected void onResume() {
super.onResume();
}
public void init() {
ScanProduct = (CardView) findViewById(R.id.scan_product);
SearchProduct = (CardView) findViewById(R.id.search_product);
// Search On Button Click
ScanProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speakOut("You have Just pressed Scan Option");
}
});
SearchProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speakOut("You have Just pressed Search Option ");
}
});
}
#Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
#Override
public void onInit(int status) {
int result = tts.setLanguage(Locale.US);
if (status == TextToSpeech.SUCCESS) {
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
} else {
speakOut("");
}
} else if (status == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...",
Toast.LENGTH_LONG).show();
}
}
private void speakOut(String text) {
tts.setPitch(1.0f); //Normal Pitch
tts.setSpeechRate(0.7f); // 1.0 is Normal speech Rate
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
private void SpeakOutOnce(String text) {
if (tts != null) {
tts.setPitch(1.0f); //Normal Pitch
tts.setSpeechRate(0.7f); // 1.0 is Normal speech Rate
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
}