In Android how can I take multiple images using ImageReader class

227 Views Asked by At

I am trying to take multiple images but the ImageAvailabeListener function is not moving forward. I guess it is waiting for next image. I tried aquireNextImage() but it is also not working.

I'm taking images while MediaProjection.

class ImageAvailableListener implements ImageReader.OnImageAvailableListener {
    @Override
    public void onImageAvailable(ImageReader reader) {
        Image image = null;
        FileOutputStream fos = null;
        bitmap = null;
        try {
            image = mImageReader.acquireLatestImage();
            Log.d("servicecheck", "image" + image);
            Log.d("imagecheck", "" + image);
            if (image != null) {
               

                Image.Plane[] planes = image.getPlanes();
                ByteBuffer buffer = planes[2].getBuffer();
                int pixelStride = planes[2].getPixelStride();
                int rowStride = planes[2].getRowStride();
                int rowPadding = rowStride - pixelStride * mWidth;

                // create bitmap
                bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
                
                bitmap.copyPixelsFromBuffer(buffer);
               
                // fix the extra width from Image
                Bitmap croppedBitmap;
                try {
                    croppedBitmap = Bitmap.createBitmap(bitmap, 0, 0, mWidth, mHeight);
                } catch (OutOfMemoryError e) {
                   
                    croppedBitmap = bitmap;
                }
                if (croppedBitmap != bitmap) {
                    bitmap.recycle();
                }

                // write bitmap to a file
               
                storeDirectory = new File(mStoreDir);
                storeDirectory.mkdir();
             

                fos = new FileOutputStream(storeDirectory.getAbsolutePath() + "/myscreen_" + Calendar.getInstance().getTime() + ".png");
                croppedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            

                //IMAGES_PRODUCED++;
                fos.flush();
                fos.close();
                
                stopProjection();
                stopSelf();
                scanFile(getApplicationContext(), Uri.fromFile(storeDirectory));
                Log.d("stoptaskcheck", "stopSelf");

            } else {
                Log.d("servicecheck", "null image" + image);

            }

        } catch (Exception e) {
            if (bitmap != null) {
                bitmap.recycle();
            }
            e.printStackTrace();
        }

    }
}

//In the below function I have created set maxImages to 20(more than 1)

@SuppressLint("WrongConstant")
private void createVirtualDisplay() {

    // start capture reader

    mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 20);
    mVirtualDisplay = mMediaProjection.createVirtualDisplay(SCREENCAP_NAME, mWidth, mHeight,
            mDensity, getVirtualDisplayFlags(), mImageReader.getSurface(), null, null);
    mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), null);
}
0

There are 0 best solutions below