integrating with intents for ocr and zxing - java

I have an app, i used this code to integrate zxing
public Button.OnClickListener mScan = new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
};
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
I have both zxing scanner as well as google goggles installed in my mobile phone. When i start the app and try to scan, I get the option to choose either the barcode scanner or the goggle app. I thought, hey let's try and use the goggle app for doing other stuff as well like OCR. I select the goggle option but the app does not have the take picture option within it. How do I integrate goggles also with my app? with full functionality?

I have no idea about how to integrate your app with Google Goggles. However, if you looking for an app that provide OCR function, you may use my app:
https://play.google.com/store/apps/details?id=sunbulmh.ocr
Here is a sample code that you can use in your app to get OCR service:
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo("sunbulmh.ocr", PackageManager.GET_ACTIVITIES);
Intent LaunchIntent = pm.getLaunchIntentForPackage("sunbulmh.ocr");
LaunchIntent.setFlags(0);
startActivityForResult(LaunchIntent,5);
} catch (NameNotFoundException e) {
Uri URLURI = Uri.parse("http://play.google.com/store/apps/details?id=sunbulmh.ocr");
Intent intent = new Intent(Intent.ACTION_VIEW,URLURI);
startActivity(intent);
}
Then, get the result in onActivityResult():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == 5){
String ocr_txt = data.getStringExtra(Intent.EXTRA_TEXT);
// ocr_txt contains the recognized text.
}
}
}

Related

How to get Callback share result in android

How to get the intent callback sharing result, i have seen some code and implemented from my side. Below is the sample code
try {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivityForResult(Intent.createChooser(intent, "Choose"), 1);
} catch (Exception e) {
e.printStackTrace();
}
Code for callback result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
}
}
}
In this requestCode is always getting 0 not 1 after i successfully share to any social media platform, is there any code which is newer version?
The method you're using is deprecated, The new way is
ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
// do something
}
});
Launching the activity
launcher.launch(yourIntent);
sharing result
Intent intent = new Intent();
intent.putExtra(YOUR_KEY, YOUR_DATA);
setResult(RESULT_OK, intent);
Dependency
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation "androidx.activity:activity:1.2.0"
...
}
To learn more about this visit

Call method when intent closes

I am making a music player application, and I am trying to implement playlists. I have a file chooser in another intent, and I would like the ListView in the mainActivity to update when the file chooser intent closes. how can I call my UpdateListView method when it closes?
start intent:
Intent intent = new Intent(this, FileChooser.class);
startActivity(intent);
Closing intent
public void closeButton(View view){
finish();
}
Any help would be appreciated! thanks!
I assume you are using your own FileChoser class, not a standard Android one:
private static final int FileChooserRequestCode = 666;
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FileChooserRequestCode);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FillChooserRequestCode) {
if (resultCode == Activity.RESULT_OK) {
// ... file is chosen
String fileName = data.getStringExtra("FileName");
} else {
... dialog is closed
}
}
}
in FileChoser you do
Intent intent = new Intent();
intent.putStringExtra("FileName", fileName);
SetResult(Activity.RESULT_OK, intent);
finish();
and
SetResult(Activity.RESULT_CANCELED);
finish();
You can use startActivityForResult() please refer the link Getting Results From Activity
static final int FILE_CHOOSER_INTENT = 1; // The request code
...
private void chooseFile() {
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FILE_CHOOSER_INTENT);
}
Call setResult pass your result data as Intent. for details refer link SetResult function
Override this in your calling activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == FILE_CHOOSER_INTENT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}

ZXing QR code scanner embedded pressing back button during scan issue

I have the following scenario :
I used the Maven repository from Gradle to integrate ZXing into my Android app.
In my scan activity, the code looks like this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scan_layout);
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt(" ");
integrator.setScanningRectangle(700, 700);
integrator.setResultDisplayDuration(0);
integrator.setCameraId(0); // Use a specific camera of the device
integrator.initiateScan();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = null;
scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
String scanContent = scanningResult.getContents();
if(isConnected()) {
requestdata("http://rm360project-001-site1.smarterasp.net/api/endpoint", scanContent);
}else {
Toast.makeText(this, "Internet Connection not available", Toast.LENGTH_LONG).show();
}
} else {
Intent getMainScreen = new Intent(ScanScreen.this, MainActivity.class);//pentru test, de sters
startActivity(getMainScreen);
}
}
The way I want it to work :
1. If I scan a QR code, call the function requestdata
2. If I press back during scan, go to MainActivity
The problem :
Even when I press back on my device, the function requestdata is called, I think because scaningResult is never null. Shouldn't it be null when back is pressed?
Do you have any ideea why this happens?
Thank you!
Don't know if your still interested but...
Simply change this line:
if (scanningResult != null) {
To this:
if (scanningResult != null && resultCode==RESULT_OK) {
For some reason simply scanningResult does not actually return null as suggested by the ZXing team, even when the Intent is canceled.

Intent.createChooser android QR code reader

I want the user to choose a QR reader from his installed apps. This could be done by using the Intent.createChooser. When a picture is taken with the QR reader, the QR code should be sent back to my application. This is what Ive tried so far:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
String title = (String) getResources().getText(R.string.chooser_title);
Intent chooser = Intent.createChooser(intent, title);
startActivityForResult(chooser, CUSTOM_REQUEST_QR_SCANNER);
The scanner doens't start correctly, it only shows an sample QR code. I have a feeling the intent.setType("text/plain") might be wrong? What type is a QR reader? Or how do i start a QR reader this way correctly?
I also have an ActivityResult when the QR app is done:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == CUSTOM_REQUEST_QR_SCANNER) {
Log.d(TAG, "QR activity complete");
//Successful scan
if (resultCode == RESULT_OK) {
Replace
intent.setType("text/plain");
with
intent.setType("com.google.zxing.client.android.SCAN");
Follow This Demo and do include "android-integration.jar" in your project it has this .jar file also... and also you can download Zxing Library from Here it will use the available QR Code Scanner in your app. There are also other ways just use this first you will get to know other also by R and D.
OR
Use this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_scan =(Button) findViewById(R.id.btn_scan);
btn_scan.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (result != null) {
String contents = result.getContents();
if (contents != null) {
showDialog("Found QRcode", result.toString());
} else {
showDialog("NO QRcode Found", "The user gave up and pressed Back");
}
}
}
private void showDialog(String title, CharSequence message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton("OK", null);
builder.show();
}
and include same .jar file in project properties java build path. you can download .jar from here same link.

Building a Camera App - Receive

I am new to Android programming and I'm writing an application in Java that opens the camera take a photo and save it. I made it via Intents but I can't see onActivityResult running.
I have tested it into my phone (Samsung Galaxy S) and when I take the photo I receive a preview of that photo having two buttons one Save and the other Cancel. I haven't added something to my code to do this so I think it's something that camera does. I want after capturing the image to run onActivityResult (after I press the Save button on the preview).
But how I'm going to return a result to start onActivityResult after pressing the Button Save on the preview?
I FORGOT to tell that after i press save my entire app is terminated.
Here is my Code
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TakePicButton = (Button) findViewById(R.id.TakePicture);
TakePicButton.setOnClickListener((android.view.View.OnClickListener) this);
}
#Override
public void onDestroy(){
super.onDestroy();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.TakePicture){
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
try the below code, you will have to modify it a bit, it will help you get From Library and From Camera both, the SELECT_PICTURE is used for getting image from library
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case SELECT_PICTURE:
Uri selectedImageUri = data.getData();
filemanagerstring = selectedImageUri.getPath();
selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null)
myFile = new File(selectedImagePath);
else if (filemanagerstring != null)
myFile = new File(filemanagerstring);
if (myFile != null) {
Bitmap bmp_fromGallery = decodeImageFile(selectedImagePath);
break;
case CAMERA_REQUEST:
Bitmap bmp_Camera = (Bitmap) data.getExtras().get("data");
break;
default:
break;
}
}

Categories

Resources