0
비트 맵을 사용하여 Android 화면을 캡쳐 한 다음 이미지로 변환하고 싶습니다. 메소드는 정적이어야합니다.검은 화면 캡쳐 화면
이것은 버튼 클릭을 등록한 코드입니다. "takeScrernshot"을 클릭하면 TextureView
이 캡처되어 Bitmap으로 변환 된 다음 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>
로그에 ** 찍은 ** 스크린 샷을 보았습니까? 'getBitmap (...)'은 레이아웃에서 주석 처리 한 것처럼 보이는'SurfaceView'의 비트 맵 표현을 얻을 것입니다. – Titus
@Titus, 주석이 없습니다. getBitmap에서 SurfaceView가 아닌 TexturteView의 스크린 샷을 찍습니다. – GuessWho
[DOCUMENTATION] (https://developer.android.com/reference/android/view/TextureView.html)과 같이 말입니다. 'getBitmap (...)'메소드 ** 연관된 표면 텍스처의 내용을 나타내는 비트 맵 표현을 반환합니다. 표면 텍스처를 사용할 수없는 경우이 메서드는 null을 반환합니다. **. 레이아웃에서'SurfaceView'는''으로 둘러싸여 있습니다 (즉, 주석 처리됩니다). – Titus