Is there a way of displaying 2 input for QR code in android - android-studio

So I’m creating an android app for queuing and it uses a QR code for reference and when scanned should display date and time of the chosen schedule but my code only displays one value when scanned
This is my code
String name;
setContentView(R.layout.deposit_gen);
dl_dep = findViewById(R.id.deposit_download);
qr_dep = findViewById(R.id.qr_deposit);
try {
bitmap = textToImageEncode(select_date.getText().toString().trim());
qr_dep.setImageBitmap(bitmap);
dl_dep.setVisibility(View.VISIBLE);
dl_dep.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "code_scanner",
null);
Toast.makeText(Deposit.this, "Saved to Gallery", Toast.LENGTH_LONG).show();
}
});
select_date is the variable for chosen dates and time is time

Related

Toast Message will not display if placed at the beggining of the try catch

I am having an issue with displaying a toast message. I want it to display when a button is initially clicked, so that the user is aware the app is working( in my case, generating a QR code). If I place the toast at the beginning of the try catch it does not display right away when the button is clicked. It displays the toast only after the QR code is generated and displayed. Furthermore, the toast is displayed(after QR code is displayed) on my Android emulator but not at all on my physical android device(I have allowed app permissions) if I place it at the beginning of the try catch. It will only display on my physical device if the toast is at the end of the try catch. This has me puzzled. I attempted to use a ProgressBar that would run on the button click and terminate after the QR code was displayed but found this difficult to implement. I thought the toast would be an easy alternative as I have used them through out my project without any issues until now. If anyone can help me understand how I can get the toast to display right away when the button is clicked instead of displaying after the QR code is generated and displayed, it would much appreciated. Alternatively, if someone could assist me with including a progress bar(circle) to run once the button is clicked and stop once the QR code is displayed that would also be very much appreciated, as I believe this would be the most ideal solution.
Please see code below:
public class Generate extends AppCompatActivity {
public final static int QRcodeWidth = 500 ;
private static final String IMAGE_DIRECTORY = "/QR_Code_Generated";
Bitmap bitmap;
private EditText etQr;
private ImageView ivGenerated;
private Button btn;
private Button clrBtn;
private Button shareBtn;
private ProgressBar progressBar;
private int progressStatus = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generate);
ivGenerated = (ImageView) findViewById(R.id.iv_generated);
etQr = (EditText) findViewById(R.id.generate_input);
btn = (Button) findViewById(R.id.button);
clrBtn = (Button) findViewById(R.id.clearButton);
shareBtn = (Button) findViewById(R.id.shareButton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (etQr.getText().toString().trim().length() == 0) {
} else {
try {
Toast toast1 = Toast.makeText(Generate.this, "Please Wait, Your QR Code is Generating & will be Saved to your Device... ", Toast.LENGTH_SHORT);
toast1.show();
bitmap = TextToImageEncode(etQr.getText().toString());
ivGenerated.setImageBitmap(bitmap);
String path = saveImage(bitmap); //give read write permission
// Toast.makeText(Generate.this, "QR Code saved to -> " + path, Toast.LENGTH_SHORT).show(); (if I include this second toast it does not display the first toast1)
} catch (WriterException e) {
e.printStackTrace();
}
}
}
});
I dont find any issue with your code for toast pop up, you can try adding a delay of 250ms after toast.show(), maybe it . would be a work around for you.
And for progress bar, its very simple. Create a progress bar in your .xlm file n place it at the center of your View.Layout you willing to show it & make its visibility as FALSE.
You can try this,
progressbar = = (ProgressBar) findViewById(R.id.progressbar);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressbar.setVisibility(VISIBLE); // or try View.VISIBLE
if (etQr.getText().toString().trim().length() == 0) {
} else {
try {
Toast toast1 = Toast.makeText(Generate.this, "Please Wait, Your QR Code is Generating & will be Saved to your Device... ", Toast.LENGTH_SHORT);
toast1.show();
bitmap = TextToImageEncode(etQr.getText().toString());
ivGenerated.setImageBitmap(bitmap);
String path = saveImage(bitmap); //give read write permission
// Toast.makeText(Generate.this, "QR Code saved to -> " + path, Toast.LENGTH_SHORT).show(); (if I include this second toast it does not display the first toast1)
} catch (WriterException e) {
e.printStackTrace();
}
finally {
progressbar.setVisibility(INVISIBLE); // or try View.INVISIBLE
}
}
}
});

can't take multiple Images using camerax

I am trying to take multiple pictures using camerax but only the first picture is taken, code and log output will show what I mean.
Here is the code :
Log.d(TAG, "------------------ taking new picture1");
mImageCapture.takePicture(new ImageCapture.OnImageCapturedListener() {
#Override
public void onCaptureSuccess(ImageProxy imageProxy, int rotationDegrees) {
Image image = imageProxy.getImage();
Log.d(TAG, "taking new picture onCapture Success 1 called");
}
#Override
public void onError(ImageCapture.UseCaseError useCaseError, String message, #Nullable Throwable cause) {
super.onError(useCaseError, message, cause);
Log.d(TAG, "--------- error in image capture 1" + message);
}
});
Log.d(TAG, "------------------ taking new picture 2");
mImageCapture.takePicture(new ImageCapture.OnImageCapturedListener() {
#Override
public void onCaptureSuccess(ImageProxy imageProxy, int rotationDegrees) {
Image image = imageProxy.getImage();
Log.d(TAG, "taking new picture onCapture Success 2 called");
}
#Override
public void onError(ImageCapture.UseCaseError useCaseError, String message, #Nullable Throwable cause) {
super.onError(useCaseError, message, cause);
Log.d(TAG, "--------- error in image capture 2" + message);
}
});
The relevant log output is:
2019-09-04 12:23:00.978 28970-29006/com.example.david.digified_android D/ScanDocumentFragment: ------------------ taking new picture1
2019-09-04 12:23:00.980 28970-29006/com.example.david.digified_android D/ScanDocumentFragment: ------------------ taking new picture 2
2019-09-04 12:23:02.063 28970-28970/com.example.david.digified_android D/ScanDocumentFragment: taking new picture onCapture Success 1 called
but taking new picture onCapture Success 2 called never happens
although according to documentation taking two pictures is not wrong :
TakePicture returns immediately and a listener is called to provide the results after the capture completes. Multiple calls to takePicture will take pictures sequentially starting after the previous picture is captured.
https://developer.android.com/reference/androidx/camera/core/ImageCapture?hl=en
It seems this is an issue with the library, and here's the bug:
https://issuetracker.google.com/issues/140518887
Update
It seems it's not a bug according to the comments on the issue by the team and the problem is that I have to call image.close(); when I finish processing so that my code should be :
Log.d(TAG, "------------------ taking new picture1");
mImageCapture.takePicture(new ImageCapture.OnImageCapturedListener() {
#Override
public void onCaptureSuccess(ImageProxy imageProxy, int rotationDegrees) {
Image image = imageProxy.getImage();
Log.d(TAG, "taking new picture onCapture Success 1 called");
image.close();
}
});
Log.d(TAG, "------------------ taking new picture 2");
mImageCapture.takePicture(new ImageCapture.OnImageCapturedListener() {
#Override
public void onCaptureSuccess(ImageProxy imageProxy, int rotationDegrees) {
Image image = imageProxy.getImage();
Log.d(TAG, "taking new picture onCapture Success 2 called");
image.close();
}
});
Phenomenon problem:
After calling mImageCapture.takePicture () twice in a row, there is no response after calling mImageCapture.takePicture () again. If it exits, it will enter the onError callback, indicating that the camera cannot be found.
In this case, you will find that the console has two messages as follows:
D/ImageCapture: Send image capture request [current, pending] = [0, 1].
W/ImageCapture: Too many images acquired. Close the image so that the next image can be processed.
reasons:
The captured image is too much and must be closed to run down. Then, look at the source code of ImageCapture and search for imageProxy.close(), and found that there are four places: two places are called during capture and two places are judging not called at the moment, that is, there is no close after taking a picture and return successfully under normal circumstances, so this will cause this problem.
Solution:
In the successful callback function of mImageCapture.takePicture (), manually close the image after using it, and this problem can be solved.
mImageCapture.takePicture(new ImageCapture.OnImageCapturedListener(){
#Override
public void onCaptureSuccess(ImageProxy imageProxy, int rotationDegrees) {
//Do something
Log.d(TAG, "taking new picture onCapture Success 1 called");
imageProxy.close();
}
#Override
public void onError(ImageCapture.UseCaseError useCaseError, String message, #Nullable Throwable cause) {
super.onError(useCaseError, message, cause);
Log.d(TAG, "--------- error in image capture 1" + message);
}
});
resource: https://blog.csdn.net/qq_37980878/article/details/120060170

AndroidPlot FixedSizeEditableXYSeries How to use

Im new to android and very new to android plot. Can you point me to an example that uses FixedSizeEditableXYSeries?
My goal is to create a streaming plot that shows the latest sensor readings in an android app.
Thanks
===================Update - following discussion with #Nick====================
public class MainActivity extends AppCompatActivity {
// Create the redrawer so that the plot is updated
private Redrawer redrawer;
// create the message receiver - data is received via broadcasts
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("CurrentHR");
Log.d("ReceivedHR ",message);
// Now put the new data point at the end of the FixedSizeEditableXYSeries, move all data points by 1.
for (int index=0;index<9;index++){
if(index<9){
hrHistory.setY(hrHistory.getY(index+1),index);
}else{
hrHistory.setY(Float.parseFloat(message),9);
}
}
}
};
// create a few references
private XYPlot xyPlot;
private FixedSizeEditableXYSeries hrHistory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_heart_rate);
// Now find the plot views
xyPlot = (XYPlot)findViewById(R.id.xyPlot);
// Declare the local broadcast manager
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("hrUpdate"));
// now put in some data
hrHistory = new FixedSizeEditableXYSeries("HR",10);
xyPlot.addSeries(hrHistory, new LineAndPointFormatter(Color.GREEN,Color.RED,null,null));
xyPlot.setRangeBoundaries(40, 120, BoundaryMode.FIXED);
xyPlot.setDomainBoundaries(0, 20, BoundaryMode.FIXED);
}
#Override
protected void onResume(){
super.onResume();
// set a redraw rate of 1hz and start immediately:
redrawer = new Redrawer(xyPlot, 1, true);
}
}
This gives me a nice graph but no line. It doesnt look like the plot is being updates as new data is filling the FixedSizeEditableXYSeries.
If you want scrolling behavior then FixedSizeEditableXYSeries would be the wrong choice; as your data scrolls you're essentially enqueueing the newest value and dequeuing the oldest value; a linked list type structure would be a better choice.
You can either implement XYSeries and back it with any suitable data structure you prefer, or you can use SimpleXYSeries, which already supports queue operations a la removeFirst() and addLast(...). There's a great example of of a dynamic scrolling plot in the demo app: OrientationSensorExampleActivity. Lines 235-245 show the specific actions mentioned above.

duplicate view upon data change?

My CardView duplicate elements upon data change, the vardView is within a tab, and the way i declared that tab fragment as following;
in the onCreateView, i declared all the necessary firebase links and value events listeners to retrieve the required data related to the elements displayed on the cards.
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
if(snapshot !=null){
for (DataSnapshot child: snapshot.getChildren()) {
Log.i("MyTag", child.getValue().toString());
imagesfeedsList.add(child.child("address").getValue(String.class));
authorfeedsList.add(child.child("author").getValue(String.class));
ratingfeedsList.add(child.child("rating").getValue(String.class));
locationfeedsList.add(child.child("location").getValue(String.class));
publicIDfeedsList.add(child.child("public_id").getValue(String.class));
}
Log.i("MyTag_imagesDirFinal", imagesfeedsList.toString());
mImages = imagesfeedsList.toArray(new String[imagesfeedsList.size()]);
author = authorfeedsList.toArray(new String[authorfeedsList.size()]);
ratingV = ratingfeedsList.toArray(new String[ratingfeedsList.size()]);
locationV = locationfeedsList.toArray(new String[locationfeedsList.size()]);
publicID = publicIDfeedsList.toArray(new String[publicIDfeedsList.size()]);
numbOfAdrs = Long.valueOf(imagesfeedsList.size());
LENGTH = Integer.valueOf(String.valueOf(numbOfAdrs));
}
right after the snippet the adapter setup;
ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}
Then comes the view holder with a RecycleView, declaring the cardView elements. One of the elements is a ratingBar, and here where the ratingbar Listener is to submit the user rating on a specific picture.
after that the content adapter;
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
// Set numbers of List in RecyclerView.
private Context mContext;
public ContentAdapter(Context context) {
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.authorName.setText(author[position]);
holder.ratingValue.setText(ratingV[position]);
holder.locationValue.setText(locationV[position]);
Picasso.with(mContext).load(mImages[position]).into(holder.picture);
#Override
public int getItemCount() {
return LENGTH;
}
}
My problem is whenever the user submits a rating or even when the data related to any of the elements on anycard changes, the view gets duplicated ( i mean by the view, the cards ), a repetition of the cards, with the new data chnages displayed ?
i a not sure what is in my above code structure causing this and how to fix this repetitions, i mean i need the cards to be updated with the new data but not duplicated?
All right, so the problem was that every time the data changes, in the onCreate the imagesFeedList, ratingFeedList, etc does not get rid of the old information stored in it from the initial build, so when the refresh happens triggered by onDataChange, the new information gets added to the previous information, which cause the view to repeat the cards, thus just at the beginning of onDataChange and before storing any information in the several feedLists, it must be cleared;
imagesfeedsList.clear();
authorfeedsList.clear();
ratingfeedsList.clear();
locationfeedsList.clear();
publicIDfeedsList.clear();
and by that i made sure the view does not repeat build up based on old information.

use thread inside buttonclick in android

I am receiving temperature values from a sensor through BLE and storing it in my db and passing the values as an array through intent to graph activity and diplaying a line graph. I am also posting the values to google app engine. i am doing this all within runOnUiThread() and hence my app crashes when there are too many values.Is it possible to pass values into the graph activity on a new thread? or any other better suggestions?
#Override
public void propertyChange(final PropertyChangeEvent event) {
final String property = event.getPropertyName();
runOnUiThread(new Runnable() {
public void run() {
try {
if (property.equals(PROPERTY_IR_TEMPERATURE)) {
double newIRValue = (Double) event.getNewValue();
// float newIRValue_1 = (Float) event.getNewValue();
TextView textView = (TextView) findViewById(R.id.ir_temperature);
String value = decimal.format(newIRValue);
String formattedText = value + DEGREE_SYM;
float value_chk = (float)(newIRValue);
String formattedText_1 = String.valueOf(value_chk);
String sensorid = test_1;
String sensortype = "temperature";
// write_data(sensorid,sensortype,value);
// Toast.makeText(getBaseContext(), "ARV sensid:"+sensorid+" senstype:"+sensortype, Toast.LENGTH_SHORT).show();
long timemilli = System.currentTimeMillis();
//Log.d("TIMMEE", String.valueOf(timemilli));
String time=String.valueOf(timemilli);
db = new DBHandler(getApplicationContext());
//db.deleteTempReadings();
// inserting data to temp table
Log.i("insert", "Inserting Records...");
);
Temperature(sensorid,formattedText_1));
//Toast.makeText(getBaseContext(), "SENSOR_ID:"+t.getSensorId()+"TEMPERATURE: "+t.getTemperature()+"TIME: "+t.getTimestamp(), Toast.LENGTH_LONG).show();
// reading data from db
Log.i("Reading", "Reading Records...");
List<Temperature> temp = db.getAllTempReadings();
int arraySize=temp.size();
double tempArray[]=new double[arraySize];
int timeArray[]=new int [arraySize];
//Log.d("ans", String.valueOf(temp.size()));
for (Temperature t : temp) {
String log="SENSOR ID: "+t.getSensorId()+"TEMPERATURE: "+t.getTemperature()+"TIME: "+t.getTimestamp();
Log.d("Record"+t, log);
}
// Reading values into the array!
for(int i=0;i<arraySize;i++){
tempArray[i]=Double.parseDouble(temp.get(i).getTemperature());
}
Button btn=(Button)findViewBy Id(R.id.btnGraph);
btn.setOnCLickListener(new View.onClickListener(){
public void onClick(View v){
Intent iIntent =new Intent(getApplicationContext(),GraphActivity.class);
tIntent.putExtra("tempArray",tempArray);
startActivity(tIntent);
}
});
// rest....posting to server code......
textView.setText(formattedText);
}
}
}
}
}
The temperature value you get from the sensor upto this you have to do stuff in one thread and after that you have to start three thread one for the inserting into the DB, the other one for the displaying the line graph and the last and the third one for the posting the data into the google app engine.
I think you are a bit confused here. The idea is to keep slow processing OUT of your UI thread. I suggest you do the writes to the DB and the App engine first in the 'current' (presumably less critical) thread and then when all is done simply update the graph display in the UI thread.
The only reason you need RunOnUiThread is to make changes to the views on the screen.

Resources