Java 截图时的黑色图像
我想用位图捕捉我的Android屏幕,然后将其转换成图像。这些方法应该是静态的 这是我的代码,我在其中注册了一个按钮点击,点击后,“takeScrernshot”捕获Java 截图时的黑色图像,java,android,android-studio,bitmap,screenshot,Java,Android,Android Studio,Bitmap,Screenshot,我想用位图捕捉我的Android屏幕,然后将其转换成图像。这些方法应该是静态的 这是我的代码,我在其中注册了一个按钮点击,点击后,“takeScrernshot”捕获TextureView并将其转换为位图,然后将其保存到文件中 public class MainActivity extends AppCompatActivity { // View rootView = getWindow().getDecorView().getRootView(); private sta
TextureView
并将其转换为位图,然后将其保存到文件中
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:**
public类MainActivity扩展了AppCompatActivity{
//视图根视图=getWindow().getDecorView().getRootView();
私有静态TextureView TextureView;
按钮;
SurfaceView mPreview;
私人土地持有人;
私有静态文件截图文件;
专用静态浮动视频宽度=1080.0f;
专用静态浮动视频高度=1920.0f;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textureView=(textureView)findViewById(R.id.textureView);
按钮=(按钮)findViewById(R.id.button);
//mPreview=(SurfaceView)findViewById(R.id.surface);
//holder=mPreview.getHolder();
//holder.setFormat(PixelFormat.RGBX_8888);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//截图();
takeScreenshotVideoPlaying();
}
});
}
公共静态位图截图视频播放(){
Log.d(“,”VideoPlayerActivity running中的takeScreenshotVideoPlaying());
位图=空;
int=100;
for(int i=1;i
我有一个纹理视图,我需要注册点击,并在将位图转换为文件后创建一个屏幕截图(仅用于测试)——图像是黑色的
<?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
,它渲染视频,所以当我尝试获取屏幕的位图时,由于图像每次都在变化,我得到了黑屏。为了捕获视频,我使用了两种不同的方法:
-对于根设备,我用鼠标光标或屏幕上的其他任何东西捕获了整个屏幕:
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
的内置方法:
位图bm=mmediplayer.getCurrentFrame()
您在日志中看到过截图吗?getBitmap(…)
将为您提供一个SurfaceView
的位图表示,您似乎已经在布局中注释掉了它。@Titus,它没有注释。在getBitmap中,我拍摄了TextureView的屏幕截图,而不是表面视图。getBitmap(…)
方法返回关联表面纹理内容的位图表示形式。如果表面纹理不可用,此方法将返回null。。在您的布局中,SurfaceView
被
包围,这意味着它被注释掉(排除在外)。@Titus,即使在课堂上未注释和被注释,也会出现黑屏。恐怕我没有从位图转换到文件