I cannot use the result from Zxing to do my activity - java

I am new to Android, i want to use the data from QR code to set my textview.
and i want to test the data's result as well.
the QR code format is just plain Text.
the problem is after I scanned my text wasn't change.
please help me check the code
btnSendJob.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
//result from Zxing
#SuppressWarnings("unused")
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
TextView text1 = (TextView) findViewById(R.id.resultQR);
TextView text2 = (TextView) findViewById(R.id.textToSelectGroup);
text1.setText(contents.toString());
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
btnSendJob.setText("failed");
}
}
}
});

I think something like this works:
import zxing.IntentIntegrator;
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
String barcode = result.getContents();
//yadda, yadda..
}
}

Related

How to implement speech to text in my app

I am building an app that utilities speech to text, in my code everything seems fine till it gets to protected void onActivityResult method to handle the results, it generates an error saying onActivityResult is a variable and then if I delete the access modifier, it sees it as a method then it generates another error in the parameters saying identifier expected and token is missing
Any help would be greatly appreciated.
My code
private static final int SPEECH_REQUEST_CODE = 100;
// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
List<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
Output.setText(results.get(0));
}
}
DO it like this:
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
And in onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
And in onCreate:
//Button you craete to start speech recognition
Button btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
// hide the action bar
//getActionBar().hide();
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displaySpeechRecognizer();
}
});

how do I get my intent to work with Google Place Autocomplete?

Can you tell me what I need to do to make my intent work please? When the user clicks mEditInit, the Places drop down menu appears.
When the user clicks the place in the menu, I want to send this back to mEditInit
I've used intents before but not working in this case.
public class MyActivity extends AppCompatActivity {
String stuff;
private EditText mEditInit;
public static final int AUTOCOMPLETE_REQUEST_CODE = 1;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mEditInit = (EditText) findViewById(R.id.edit_message);
mEditInit.setX(0);
mEditInit.setY(250);
//Get the bundle
Bundle bundle = getIntent().getExtras();
if (stuff != null && !stuff.isEmpty())
//Extract the data…
{ stuff = bundle.getString("stuff");
mEditInit.setText(stuff);
}
Places.initialize(this, getString(R.string.places_api_key));
mEditInit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "bowwow", Toast.LENGTH_SHORT).show();
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fields).build(MyActivity.this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
//setResult(RESULT_OK, intent);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
//Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();
Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(this, MyActivity.class);
String getrec= place.getName();
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("stuff", getrec);
//Add the bundle to the intent
i.putExtras(bundle);
// Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
// Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
// super.onActivityResult(requestCode, resultCode, data);
}
}
}
In your onActivityResult method, you do not need any extra.
Just call the method setText() on your EditText:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
//Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();
mEditInit.setText(place.getName());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
// Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
// super.onActivityResult(requestCode, resultCode, data);
}
}

Integrate git project on an existing project startActivityForResult and onActivityResult android

I implemented this git on my current project without cloning
implementation 'com.github.adityaarora1:LiveEdgeDetection:master-SNAPSHOT'
But I'm unable to call it on my method. The document says
Start startActivityForResult from your activity
startActivityForResult(new Intent(this, ScanActivity.class), REQUEST_CODE);
Get a file path for cropped image on onActivityResult
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
So I tried calling like this onClick button from a new Class
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this,
ScanActivity.class);
startActivity(myIntent);
}
and put the rest inside my onActivityResult
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
}
Edit: here is the MainActivity the author used on git I tried using it I get this error:
scannedImageView = findViewById(com.adityaarora.liveedgedetection.R.id.scanned_image);
MainActivity (imported)
private static final int REQUEST_CODE = 101;
private ImageView scannedImageView;
Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
scannedImageView = findViewById(com.adityaarora.liveedgedetection.R.id.scanned_image);
startScan();
scan = findViewById(R.id.open_scan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(ScanActivity2.this,
ScanActivity.class);
startActivityForResult(myIntent ,111);
}
});
}
private void startScan() {
Intent intent = new Intent(this, ScanActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK) {
if(null != data && null != data.getExtras()) {
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
scannedImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
scannedImageView.setImageBitmap(baseBitmap);
}
} else if(resultCode == Activity.RESULT_CANCELED) {
finish();
}
}
}
Update :
After some research I found that the imported project was on read file only and cannot be changed (ScanActivity.java) and my current project was updated sdk 28 which is different from the one Imported so there is some errors in ScanActivity which Is why the button (technically) wasn't working
You should use startActivityForResult instead of startActivity like below.
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this,
ScanActivity.class);
startActivityForResult(myIntent ,111);
}
});
and modify your onActivityResult like
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == 111){
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
Log.d("YourTAG","File Path "+filePath);
// here you can set bitmap to your image view
yourImageView.setImageBitmap(baseBitmap);
}
}
UPDATE
You Main Activity should be like
private static final int REQUEST_CODE = 111;
private ImageView scannedImageView;
Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
scannedImageView = findViewById(R.id.scanned_image); // this ImageView should be in your activity_scan.xml file with same id(scanned_image)
startScan();
scan = findViewById(R.id.open_scan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startScan();
}
});
}
private void startScan() {
Intent intent = new Intent(ScanActivity2.this, ScanActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK) {
if(null != data && null != data.getExtras()) {
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
scannedImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
scannedImageView.setImageBitmap(baseBitmap);
}
} else if(resultCode == Activity.RESULT_CANCELED) {
finish();
}
}
}
Demo project
I have uploaded a demo project on Github which is integrated LiveEdgeDetection library and working as expected.
To check it go here

How to recrop the selected image in android?

I am cropping the selected image from gallery or camera successfully by using the following source code. But I want to recrop the selected image without capturing the image again. Could you please anyone guide me to do this task
implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0'
btn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
clearData();
CropImage.activity().start(MainActivity.this);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
CropImage.ActivityResult result =
CropImage.getActivityResult(data);
if (resultCode == RESULT_OK){
picUri = result.getUri();
imageview.setImageURI(picUri);
BitmapDrawable bitmapDrawable =
(BitmapDrawable)imageview.getDrawable();
bitmap = bitmapDrawable.getBitmap();
saveFaceToGallery();
showTextfromImage();
}
else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
{
Exception e = result.getError();
}
}
}

Android Java, Setting text of a field outside of the application

I'm currently writing an Android application that will allow a user to input the result of a scanned barcode into a field outside of the application.
The app will simply be a Floating Button that when pressed, will bring up a camera screen that scans and reads the barcode being pointed at, and save the result as an intent.
The user will primarily be working on Chrome when using this Floating Button.
Question: Is it possible to set the text of the current field in focus, outside of the application, as the result of the scanned barcode?
I'm using the following libraries:
- com.google.zxing:core:3.3.3
- com.journeyapps:zxing-android-embedded:3.5.0#aar
Current code:
public class MainActivity extends AppCompatActivity {
FloatingActionButton fabScan;
TextView textViewScanResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity activity = this;
textViewScanResult = findViewById(R.id.textViewScan);
fabScan = findViewById(R.id.floatingActionButton_scan);
fabScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan Barcode");
integrator.setCameraId(0);
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.setOrientationLocked(false);
integrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
} else {
textViewScanResult.setText(result.getContents());
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Currently, i'm only setting a textView as the scanned result.
try setting browser intent with the result content.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
} else {
textViewScanResult.setText();
//here
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.stackoverflow.com")); //change url to google
intent.putExtra(result.getContents().toString());
startActivity(intent);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}

Categories