Merge pull request #195 from nitta-lab-2021/ScreenShot
確認!!
commit efabb154acee60bf2ecf54bad7339632d2bd2965
2 parents 2f64f16 + 639271f
赤木元基 authored on 8 Oct 2021
Showing 2 changed files
View
245
app/src/main/java/org/ntlab/acanthus_client/views/paint/SendScreenShotActivity.java
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.app.Activity;
import android.graphics.ImageFormat;
import android.os.Bundle;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.graphics.PixelFormat;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
 
import org.ntlab.acanthus_client.R;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
 
public class SendScreenShotActivity extends Activity implements View.OnClickListener {
import static android.graphics.ImageFormat.JPEG;
 
 
public class SendScreenShotActivity extends Activity {
 
private MediaProjectionManager mpManager;
private MediaProjection mProjection;
private static final int REQUEST_MEDIA_PROJECTION = 1001;
 
private int displayWidth, displayHeight;
private ImageReader imageReader;
private VirtualDisplay virtualDisplay;
private int screenDensity;
private ImageView imageView;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_screen_shot);
// 全体キャプチャ
Button captureAll = (Button)findViewById(R.id.capture_all);
captureAll.setOnClickListener(this);
 
// アイコン画像のみキャプチャ
Button captureIcon = (Button)findViewById(R.id.capture_icon);
captureIcon.setOnClickListener(this);
Button button = findViewById(R.id.capture);
// ボタンタップでスクリーンショットを撮る
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getScreenshot();
}
});
 
// キャプチャした画像を添付してメールを送る
Button sendMail = (Button)findViewById(R.id.send_mail);
sendMail.setOnClickListener(this);
// 撮影したスクリーンを表示するImageView
imageView = findViewById(R.id.image);
 
// 画面の縦横サイズとdpを取得
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
screenDensity = displayMetrics.densityDpi;
displayWidth = displayMetrics.widthPixels;
displayHeight = displayMetrics.heightPixels;
 
mpManager = (MediaProjectionManager)
getSystemService(MEDIA_PROJECTION_SERVICE);
 
// permissionを確認するintentを投げ、ユーザーの許可・不許可を受け取る
if(mpManager != null){
startActivityForResult(mpManager.createScreenCaptureIntent(),
REQUEST_MEDIA_PROJECTION);
}
}
 
// ユーザーの許可を受け取る
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
 
if (REQUEST_MEDIA_PROJECTION == requestCode) {
if (resultCode != RESULT_OK) {
// 拒否された
Toast.makeText(this,
"User cancelled", Toast.LENGTH_LONG).show();
return;
}
// 許可された結果を受け取る
setUpMediaProjection(resultCode, data);
}
}
 
private void setUpMediaProjection(int code, Intent intent) {
mProjection = mpManager.getMediaProjection(code, intent);
setUpVirtualDisplay();
}
 
private void setUpVirtualDisplay() {
imageReader = ImageReader.newInstance(
displayWidth, displayHeight, 0x1, 2);
 
virtualDisplay = mProjection.createVirtualDisplay("ScreenCapture",
displayWidth, displayHeight, screenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
imageReader.getSurface(), null, null);
}
 
private void getScreenshot() {
// ImageReaderから画面を取り出す
Log.d("debug", "getScreenshot");
 
Image image = imageReader.acquireLatestImage();
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
 
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * displayWidth;
 
// バッファからBitmapを生成
Bitmap bitmap = Bitmap.createBitmap(
displayWidth + rowPadding / pixelStride, displayHeight,
Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
image.close();
 
imageView.setImageBitmap(bitmap);
}
 
@Override
public void onClick(View v) {
// 読み書きするファイル名を指定
File file = new File(Environment.getExternalStorageDirectory() + "/capture.jpeg");
// 指定したファイル名が無ければ作成する。
file.getParentFile().mkdir();
 
switch(v.getId()) {
case R.id.capture_all:
// 全体を撮る
saveCapture(findViewById(android.R.id.content),file);
break;
case R.id.capture_icon:
// View1を撮る
saveCapture(findViewById(R.id.icon),file);
break;
case R.id.send_mail:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"s1871104@s.konan-u.ac.jp"});
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(intent);
 
break;
protected void onDestroy() {
if (virtualDisplay != null) {
Log.d("debug","release VirtualDisplay");
virtualDisplay.release();
}
super.onDestroy();
}
 
/**
* 撮ったキャプチャを保存
* @param view
* @param 書き込み先ファイルfile
*/
public void saveCapture(View view, File file) {
// キャプチャを撮る
Bitmap capture = getViewCapture(view);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, false);
// 画像のフォーマットと画質と出力先を指定して保存
capture.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ie) {
fos = null;
}
}
}
}
 
/**
* キャプチャを撮る
* @param 撮りたいview
* @return 撮ったキャプチャ(Bitmap)
*/
public Bitmap getViewCapture(View view) {
view.setDrawingCacheEnabled(true);
 
// Viewのキャプチャを取得
Bitmap cache = view.getDrawingCache();
if(cache == null){
return null;
}
Bitmap screenShot = Bitmap.createBitmap(cache);
view.setDrawingCacheEnabled(false);
return screenShot;
}
 
}
View
96
app/src/main/res/layout/activity_send_screen_shot.xml
<androidx.constraintlayout.widget.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="300dp"
android:translationY="175dp"
android:layout_height="match_parent"
tools:context=".views.paint.SendScreenShotActivity">
 
<Button
android:id="@+id/capture_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_vertical_margin"
android:text="capture_all"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.285" />
 
<Button
android:id="@+id/capture_icon"
android:id="@+id/capture"
android:layout_width="136dp"
android:layout_height="52dp"
android:padding="@dimen/activity_vertical_margin"
android:text="capture_icon"
android:text="capture"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/capture_all"
app:layout_constraintVertical_bias="0.007" />
app:layout_constraintTop_toBottomOf="@+id/image"
app:layout_constraintVertical_bias="0.247" />
 
<Button
android:id="@+id/send_mail"
android:layout_width="wrap_content"
android:padding="@dimen/activity_vertical_margin"
android:text="send_mail"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/capture"
app:layout_constraintVertical_bias="0.104" />
 
<ImageView
android:id="@+id/image"
android:layout_width="269dp"
android:layout_height="436dp"
android:padding="@dimen/activity_vertical_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.987" />
 
<ImageView
android:id="@+id/icon"
android:layout_width="84dp"
android:layout_height="87dp"
android:padding="@dimen/activity_vertical_margin"
android:src="@drawable/headshot_reasonably_small_400x400"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.779" />
app:layout_constraintVertical_bias="0.206" />
 
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="心がスクショしたがってるんだ。"
android:textColor="#716D6D"
android:textColor="#03A9F4"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/capture_all"
app:layout_constraintBottom_toTopOf="@+id/capture"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
app:layout_constraintTop_toBottomOf="@+id/image"
app:layout_constraintVertical_bias="0.0" />
 
</androidx.constraintlayout.widget.ConstraintLayout>