gpt4 book ai didi

java - Android 使用 MediaProjectionManager 截屏,OnImageAvailable 无法启动

转载 作者:行者123 更新时间:2023-11-29 02:27:37 33 4
gpt4 key购买 nike

我正在开发一个 android 项目,其中还有一个位于其他应用程序之上的叠加按钮,当单击此按钮时,应截取屏幕截图。叠加按钮有效,但屏幕截图部分无效。我不知道为什么,但似乎 OnImageAvailable 没有启动。这是一个单独的项目,我只测试截图部分:

public class MainActivity extends Activity {

private static final int REQUEST_CODE= 100;

private MediaProjectionManager mProjectionManager;
private MediaProjection mProjection;
private ImageReader mImageReader;
private Handler mHandler = new Handler(Looper.getMainLooper());
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// call for the projection manager
mProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);

imageView = (ImageView) findViewById(R.id.imageView);

// start projection
final Button startButton = (Button)findViewById(R.id.button);
startButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
startProjection();
}
});

// start capture handling thread
new Thread() {
@Override
public void run() {
Looper.prepare();
mHandler = new Handler();
Looper.loop();
}
}.start();

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {

mProjection = mProjectionManager.getMediaProjection(resultCode, data);

if (mProjection != null) {
final DisplayMetrics metrics = getResources().getDisplayMetrics();
final int density = metrics.densityDpi;
final int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
final int width = size.x;
final int height = size.y;

mImageReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 2);
mProjection.createVirtualDisplay("screencap", width, height, density, flags, mImageReader.getSurface(), null, mHandler);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {

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

try {
image = mImageReader.acquireLatestImage();
if (image != null) {
final Image.Plane[] planes = image.getPlanes();

int width = image.getWidth();
int height = image.getHeight();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
byte[] newData = new byte[width*height*4];

int offset = 0;
bitmap = Bitmap.createBitmap(metrics, width, height, Bitmap.Config.ARGB_8888);
ByteBuffer buffer = planes[0].getBuffer();
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
int pixel = 0;
pixel |= (buffer.get(offset) & 0xff) << 16; // R
pixel |= (buffer.get(offset + 1) & 0xff) << 8; // G
pixel |= (buffer.get(offset + 2) & 0xff); // B
pixel |= (buffer.get(offset + 3) & 0xff) << 24; // A
bitmap.setPixel(j, i, pixel);
offset += pixelStride;
}
offset += rowPadding;
}

imageView.setImageBitmap(bitmap);
}

} catch (Exception e) {
e.printStackTrace();
} finally {

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

if (image!=null)
image.close();

}
}

}, mHandler);
}
}

super.onActivityResult(requestCode, resultCode, data);
}

private void startProjection() {
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
}

}

最佳答案

我认为你应该设置不同的 FLAGS。你告诉 MediaProjection 只显示它自己的内容,但你并没有在它的表面上画任何东西。要进行屏幕截图,您必须使用“DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR”标志复制不同的显示。

关于java - Android 使用 MediaProjectionManager 截屏,OnImageAvailable 无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51320978/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com