Reputation: 1
I am trying to take multiple images on Android java using ImageReader. It's working fine if the user only takes a single image. But if they take multiple images at the same time (click the button continuously for a short duration) the images will be pixelated and unable to use.
Below you can see the result. If I took a single image
If I took multiple images
I'm using bitmate to take the image.
byte[] imageData = ImageUtils.NV21toJPEG(ImageUtils.YUV420toNV21(image), image.getWidth(), image.getHeight(), 100);
Bitmap bitmapImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, null);
transform the bitmap into image
public static File bitmapToFile(Bitmap bitmap, String fileNameToSave, Context context) {
File file = null;
try {
file = new File(context.getExternalCacheDir() + File.separator + fileNameToSave);
file.createNewFile();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(file);
fos.write(bitmapdata);
fos.flush();
fos.close();
return file;
} catch (Exception e) {
e.printStackTrace();
return file; // it will return null
}
The reason I can think of is. When the user click the button multiple time, the image couldn't finish the process fast enough so the image got pixelated. I try to add a sleeping thread and also disable the button, but it takes so long for the image to finish the process (if I set it below 2.5s ~ 2500ms the image will still be pixelated)
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
Can someone advise me what I should do with this situation? is it the way I generate the image or Somehow I can check if the Image is perfectly generated and then allow the user to take the image again. I believe it should have been possible for the user to take multiple images in a short time.
Upvotes: 0
Views: 90