问题描述
我正在编写简单的Screenshot应用程序,并为此使用MediaProjection + ImageReader。我使用some sample解决此任务。
当我单击“捕获”按钮时,我总是在日志消息中得到"image: NULL"
。
在ImageReader.newInstance
中,我将格式设置为ImageFormat.RGB_565
。默认值为0x1
,但是当我使用此值时,会显示警告消息:
必须是以下之一:ImageFormat.UNKNowN,ImageFormat.RGB_565, ImageFormat.YV12,ImageFormat.Y8,ImageFormat.NV16,ImageFormat.NV21, ImageFormat.YUY2,ImageFormat.JPEG,ImageFormat.DEPTH_JPEG, ImageFormat.YUV_420_888,ImageFormat.YUV_422_888, ImageFormat.YUV_444_888,ImageFormat.FLEX_RGB_888, ImageFormat.FLEX_RGBA_8888,ImageFormat.RAW_SENSOR, ImageFormat.RAW_PRIVATE,ImageFormat.RAW10,ImageFormat.RAW12, ImageFormat.DEPTH16,ImageFormat.DEPTH_POINT_CLOUD, ImageFormat.PRIVATE,ImageFormat.HEIC
我的问题:为什么 ImageReader 中的 Image 总是 Null ?以及如何正确解决?谢谢
我的代码:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.ImageFormat;
import android.hardware.display.displayManager;
import android.hardware.display.Virtualdisplay;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.mediaprojectionmanager;
import android.os.Bundle;
import android.os.Environment;
import android.util.displayMetrics;
import android.util.Log;
import android.view.View;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "test_t";
private ImageReader mImageReader;
private mediaprojectionmanager mmediaprojectionmanager;
private Intent mCreateScreenCaptureIntent;
private MediaProjection mMediaProjection;
private Virtualdisplay mVirtualdisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View mBtnCapture = findViewById(R.id.btn_capture);
mBtnCapture.setonClickListener(this);
init();
}
@Override
protected void onDestroy() {
super.onDestroy();
tearDownMediaProjection();
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
mMediaProjection = mmediaprojectionmanager.getMediaProjection(resultCode,data);
if (mMediaProjection != null) {
startScreenCapture();
takeCapture();
stopScreenCapture();
}
}
@Override
public void onClick(View v) {
startActivityForResult(mCreateScreenCaptureIntent,777);
}
private void init() {
displayMetrics displayMetrics = getResources().getdisplayMetrics();
mImageReader = ImageReader.newInstance(displayMetrics.widthPixels,displayMetrics.heightPixels,ImageFormat.RGB_565,2); // format 0x1
mmediaprojectionmanager = (mediaprojectionmanager) getSystemService(MEDIA_PROJECTION_SERVICE);
mCreateScreenCaptureIntent = mmediaprojectionmanager.createScreenCaptureIntent();
}
private void startScreenCapture() {
if (mMediaProjection == null)
return;
displayMetrics displayMetrics = getResources().getdisplayMetrics();
mVirtualdisplay = mMediaProjection.createVirtualdisplay(
"ScreenCapture",displayMetrics.widthPixels,displayMetrics.densityDpi,displayManager.VIRTUAL_disPLAY_FLAG_AUTO_MIRROR,mImageReader.getSurface(),null,null);
}
private void stopScreenCapture() {
if (mVirtualdisplay == null) {
return;
}
mVirtualdisplay.release();
mVirtualdisplay = null;
}
private void takeCapture() {
Image image = mImageReader.acquireLatestimage();
if (image == null) {
Log.d(TAG,"image: NULL");
return;
}
int width = image.getWidth();
int height = image.getHeight();
final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
Bitmap mBitmap = Bitmap.createBitmap(width + rowPadding / pixelStride,height,Bitmap.Config.ARGB_8888);
mBitmap.copyPixelsFromBuffer(buffer);
mBitmap = Bitmap.createBitmap(mBitmap,width,height);
image.close();
saveBitmapToFile(mBitmap);
}
private void saveBitmapToFile(Bitmap bitmap) {
File directory = new File(Environment.getExternalStorageDirectory(),"SCREEN_TEMP");
if (!directory.exists())
directory.mkdirs();
String name = "shot" + new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date()) + "." + Bitmap.CompressFormat.PNG.toString();
File file = new File(directory,name);
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
out.flush();
out.close();
} catch (IOException e) {
e.printstacktrace();
}
}
private void tearDownMediaProjection() {
if (mMediaProjection != null) {
mMediaProjection.stop();
mMediaProjection = null;
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)