在使用Android Studio的媒体投影功能进行截屏时出现问题。

huangapple go评论95阅读模式
英文:

Problem with taking screenshots with the media projection android studio

问题

@SuppressLint("WrongConstant")
private void screenshot() {
    Point size = new Point();
    displayy.getSize(size);
    mWidth = size.x;
    mHeight = size.y;
    displayy.getMetrics(metrics);
    int mDensity = metrics.densityDpi;
    Handler handler = new Handler();

    mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        displayyy = mMediaProjection.createVirtualDisplay(
            DISPLAY_NAME, mWidth, mHeight, mDensity, 
            DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), 
            null, handler
        );
    }
    mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
        int onImageCount = 0;

        @Override
        public void onImageAvailable(ImageReader reader) {
            Image image = null;
            FileOutputStream fos = null;
            Bitmap bitmap = null;

            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    image = reader.acquireLatestImage();
                }
                if (image != null) {
                    Image.Plane[] planes = new Image.Plane[0];
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                        planes = image.getPlanes();
                    }
                    ByteBuffer buffer = planes[0].getBuffer();
                    int pixelStride = planes[0].getPixelStride();
                    int rowStride = planes[0].getRowStride();
                    int rowPadding = rowStride - pixelStride * mWidth;

                    bitmap = Bitmap.createBitmap(
                        mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888
                    );

                    bitmap.copyPixelsFromBuffer(buffer);

                    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss");
                    String formattedDate = df.format(Calendar.getInstance().getTime()).trim();
                    String finalDate = formattedDate.replace(":", "-");

                    String imgName = "/imagen" + "_" + finalDate + ".jpg";
                    String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath()
                            + "/example";
                    File dir = new File(dirPath);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }

                    String mPath = dirPath + imgName;
                    File imageFile = new File(mPath);

                    fos = new FileOutputStream(imageFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    Log.e(TAG, "captured image: ");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }

                if (bitmap != null) {
                    bitmap.recycle();
                }

                if (image != null) {
                    image.close();
                }
            }
        }
    }, handler);

    mMediaProjection.stop();
    mMediaProjection = null;
}
英文:
  1. I have this code to make screenshots. In the virtual device of android studio this code works perfectly, but in my own device Samsung(with android 8.0) when I do the apk installer the program only take some screenshots. Example I press the button to take 10 screenshots, but the program only save me 2 and the rest doesn't exist. And now I don't have any idea what's the problem.

@SuppressLint("WrongConstant")

private void screenshot() {
Point size = new Point();
displayy.getSize(size);
mWidth = size.x;
mHeight = size.y;
displayy.getMetrics(metrics);
int mDensity = metrics.densityDpi;
Handler handler=new Handler();
mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
displayyy = mMediaProjection.createVirtualDisplay(DISPLAY_NAME, mWidth, mHeight, mDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, handler);
}
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
int onImageCount = 0;
@Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
FileOutputStream fos = null;
Bitmap bitmap = null;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
image = reader.acquireLatestImage();
}
if (image != null) {
Image.Plane[] planes = new Image.Plane[0];
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
planes = image.getPlanes();
}
ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * mWidth;
bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss");
String formattedDate = df.format(Calendar.getInstance().getTime()).trim();
String finalDate = formattedDate.replace(":", "-");
String imgName = "/imagen"+ "_" + finalDate + ".jpg";
String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/example";
File dir = new File(dirPath);
if(!dir.exists()){
dir.mkdirs();
}
String mPath = dirPath + imgName;
File imageFile = new File(mPath);
fos = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Log.e(TAG, "captured image: " );
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
if (bitmap != null) {
bitmap.recycle();
}
if (image != null) {
image.close();
}
}
}
}, handler);
mMediaProjection.stop();
mMediaProjection=null;
}

答案1

得分: 1

我不知道你是否已经解决了问题,但这是我的解决方案:
该方法接收4个参数,最后一个是照片的数量。

ImageReader.newInstance(width: Int, height: Int, format: Int, maxImages: Int)

如果你看得清楚,你正在将照片的数量(2)放在最后一个参数中:

ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);

链接描述在这里

英文:

I don't know if you've already solved your problem, but here's my solution:
The method receives 4 parameters and the last one is the number of photos

enter code hereImageReader.newInstance(width: Int, height: Int, format: Int, maxImages: Int)

If you look good you are sending it in the last parameter the number of photos 2

enter code hereImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);

enter link description here

huangapple
  • 本文由 发表于 2020年9月30日 08:04:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64129214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定