can't take multiple Images using camerax - android-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

Related

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

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

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
}
}
}
});

Why does my RotateTransition throw errors after it runs for the first time?

Warning: This is my first time using threads and my first time trying out an animation. Please bear with me.
I want to rotate an ImageView. I set up a thread for it:
public class ThreadAnimation extends Thread
{
private ImageView iv;
private RotateTransition rt;
public ThreadAnimation(ImageView iv)
{
this.iv = iv;
}
#Override
public void run()
{
while (true)
{
RotateTransition r = new RotateTransition();
r.setToAngle(360);
r.setCycleCount(1);
r.setDuration(Duration.millis(300));
r.setNode(iv);
r.play();
try
{
sleep(100);
} catch (InterruptedException e)
{
return;
}
}
}
}
I call this inside my controller class, upon pressing a Button.
animation.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle (ActionEvent abschicken)
{
ThreadAnimation thread = null; //ANIMATION PIZZA
if (thread == null)
{
thread = new ThreadAnimation(olivenview);
thread.start();
}
}
});
My ImageView olivenview will rotate just like I wanted it to. However it takes quite a long time until it seems to stop (I can see it because the button triggering it still looks triggered for a while) and when I go ahead to press it a second time afterwards, I get a nonstop error stream with a lot of null pointer exceptions. I am very clueless, can anyone help me out? Is this due to my Thread Setup or does the problem lie somewhere else (in code that I didn't post here)?
I believe you do not need threads for this. Notice the .play() method returns immediately and the animation will run in the background.
That being said, try this.
...
//Create your rotation
final RotateTransition r = new RotateTransition();
r.setToAngle(360);
r.setCycleCount(1);
r.setDuration(Duration.millis(300));
r.setNode(iv);
//When the button is pressed play the rotation. Try experimenting with .playFromStart() instead of .play()
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent action) {
r.play();
}
});
...
On an other note I recommend switching to java 8 so that you can use lambda expressions instead of the anonymous class!

TextArea is not updating while zip extraction

I am working on a JavaFx application, there i have a script that extract a zip followed by some other operation like updating files etc.
I want to have a textArea that displays whats going on in background, like "Zip extracting...", "Updating xyz file" etc.
Till now i have tried following way:
MyTask<String> task;
task = new MyTask<String>() {
#Override
protected String call() throws Exception {
File path = new File(exportTo.getAbsolutePath());
updateMessage("Extracting modular app to target directory...");
patcher.unZip(appPath.getAbsolutePath(), path.getAbsolutePath());
if (path.exists()) {
AppInfo info = getAppInfo();
patcher.patchAndroid(info, resourceZip, new File(path.getAbsolutePath() + "/" + appPath.getName().substring(0, appPath.getName().lastIndexOf("."))), this);
showOkAlert("Build completed!");
} else {
showOkAlert("Modular app folder not found");
}
return "";
}
#Override
protected void updateProgress(double workDone, double max) {
patcher.reportLogs(message);
}
private String message;
#Override
public void updateMessage(final String message) {
Platform.runLater(() -> patcher.reportLogs(message));
this.message = message;
//updateProgress(0, 0);
}
};
task.run();
MyTask class
abstract class MyTask<T> extends Task<T> {
abstract public void updateMessage(String message);
}
I have tried using updateProgress method, Platform.runLater() but nothing is working.
All the message i printed in textArea are printed after all operation is done.
Please help.
As javadoc for Task states you need to manually create a Thread to execute your Task:
Thread th = new Thread(task);
th.start();
Currently your task is being run on Application UI thread and blocks UI updates.

Post render event in JavaFX

I'm trying to add a click event listener to the label of all column-headers of a TableView, as follows:
for (final Node header : tblView.lookupAll(".column-header > .label")) {
if ((header != null) && (header instanceof Label)) {
final Label headerLabel = (Label) header;
// ...
}
}
Now, the problem is that if I do this in the initialize()-function of the Controller, the Scenegraph is not yet rendered and the above code won't work. Hence my question: Is there some kind of a post-render event?
many thanks in advance.
There is a WINDOW_SHOWN event in javafx.stage.WindowEvents. That is not (imo) "Post render event" but you can utilize it in similar manner, by adding an event handler to the Stage (which extends from Window).
In the initialize method of controller class, get the primary stage and then:
stage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent window) {
Platform.runLater(new Runnable() {
#Override
public void run() {
addListenerToColumnHeaders();
}
});
}
});
Hope this helps, since didn't try myself.

Resources