我想使用位图捕获我的Android屏幕,然后将其转换为图像。方法应该是静态的。
这是我的代码,我在其中单击按钮,然后单击" TakeCrernshot"时捕获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:**
我有一个纹理视图,我需要注册水龙头,并在将位图转换为文件后创建屏幕截图(仅用于测试( - 图像是黑色
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#534783"
tools:context="com.maks.testscreenshots.MainActivity">
<TextureView
android:id="@+id/textureView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"/>
<!--<SurfaceView-->
<!--android:id="@+id/surface"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="center" />-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:background="@color/colorAccent"
android:textSize="100sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/textView" />
</android.support.constraint.ConstraintLayout>
问题是,我一直在试图屏幕截图textureView
(渲染视频(,因此,当我尝试使用屏幕的getBitmap
时,由于图像每次都会更改,因此我获得了黑屏。要捕获视频,我使用了2种不同的方法: - 对于扎根的设备,我用鼠标光标或屏幕上的其他任何内容捕获了整个屏幕:
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;
}
对于没有根的设备,我只是使用
MediaPlayer
的内置method
:bitmap bm = mmediaplayer.getCurrentFrame((;