我想使用位图捕获我的Android屏幕,然后将其转换为图像。方法应该是静态的。
这是我的代码,我在其中注册了一个按钮点击,当它被点击时#34; takeScrernshot&#34;捕获 <activity
android:name=".activity.LoginActivity"
android:hardwareAccelerated="false"
android:exported="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
并将其转换为位图,然后将其保存到TextureView
。
File
我有一个纹理视图,我需要注册点击,按钮创建一个屏幕截图(仅用于测试)将位图转换为文件后 - 图像为黑色
public class MainActivity extends AppCompatActivity {
// View rootView = getWindow().getDecorView().getRootView();
private static TextureView textureView;
Button button;
SurfaceView mPreview;
private SurfaceHolder holder;
private static File screenshotFile;
private static float videoWidth = 1080.0f;
private static float videoHeight = 1920.0f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textureView = (TextureView) findViewById(R.id.textureView);
button = (Button) findViewById(R.id.button);
// mPreview = (SurfaceView) findViewById(R.id.surface);
// holder = mPreview.getHolder();
// holder.setFormat(PixelFormat.RGBX_8888);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// takeScreenShot();
takeScreenshotVideoPlaying();
}
});
}
public static Bitmap takeScreenshotVideoPlaying() {
Log.d("", "takeScreenshotVideoPlaying() in VideoPlayerActivity running!");
Bitmap bitmap = null;
int tries = 100;
for (int i = 1; i < tries; i++) {
if (textureView.isAvailable()) {
float bitmapScale = 4.0f;
float bitmapWidth = videoWidth / bitmapScale;
float bitmapHeight = videoHeight / bitmapScale;
bitmap = textureView.getBitmap((int) bitmapWidth, (int) bitmapHeight);
if (bitmap != null) {
Log.d("", "screenshot taken");
break;
}
}
}
saveScreenshot(bitmap);
return bitmap;
}
private static void saveScreenshot(Bitmap bitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// see if this helps the uploading to GCS
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
screenshotFile = new File(mPath);
try {
if (screenshotFile.exists()) {
screenshotFile.delete();
}
if (screenshotFile.createNewFile()) {
FileOutputStream fo = new FileOutputStream(screenshotFile);
fo.write(bytes.toByteArray());
fo.close();
Log.d("Saved", "saveScreenshot: ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
**and this is how layout looks like:**
答案 0 :(得分:0)
问题是,我一直试图截取textureView
,这会渲染视频,所以当我尝试getBitmap
屏幕时,由于图像每次都在变化,我获得了黑屏。为了捕获视频我使用了两种不同的方法:
- 对于有根设备,我使用鼠标光标或屏幕上的任何其他内容捕获整个屏幕:
private static Bitmap takeScreenshot() {
Bitmap screen;
Bitmap resizedBitmap = null;
try {
Process sh = Runtime.getRuntime().exec("su", null, null);
OutputStream os = sh.getOutputStream();
Log.d("SCREENSHOT ", "PATH: " + ActFileUtil.getPathToScreenshotDirOnDevice());
os.write(("/system/bin/screencap -p " + ActFileUtil.getPathToScreenshotDirOnDevice()).getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
Log.d(TAG, "procScreenshot: SCREENSHOT TAKEN");
screen = BitmapFactory.decodeFile(ActFileUtil.getPathToScreenshotDirOnDevice());
if(screen != null) {
resizedBitmap = Bitmap.createScaledBitmap(screen, screen.getWidth() / 4, screen.getHeight() / 4, true);
}
} catch (IOException | NullPointerException | InterruptedException e) {
e.printStackTrace();
}
return resizedBitmap;
}
对于没有root的设备,我只使用method
的内置MediaPlayer
:
位图bm = mMediaPlayer.getCurrentFrame();