diff --git a/app/src/main/java/org/ntlab/acanthus_client/views/paint/SendScreenShotActivity.java b/app/src/main/java/org/ntlab/acanthus_client/views/paint/SendScreenShotActivity.java
index 732e525..a98df51 100644
--- a/app/src/main/java/org/ntlab/acanthus_client/views/paint/SendScreenShotActivity.java
+++ b/app/src/main/java/org/ntlab/acanthus_client/views/paint/SendScreenShotActivity.java
@@ -3,111 +3,137 @@
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;
- }
-
}
\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_send_screen_shot.xml b/app/src/main/res/layout/activity_send_screen_shot.xml
index 5bef23e..2d64116 100644
--- a/app/src/main/res/layout/activity_send_screen_shot.xml
+++ b/app/src/main/res/layout/activity_send_screen_shot.xml
@@ -3,34 +3,21 @@
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">
-
-
+ app:layout_constraintTop_toBottomOf="@+id/image"
+ app:layout_constraintVertical_bias="0.247" />
-
-
+
+
+ app:layout_constraintVertical_bias="0.206" />
+ app:layout_constraintTop_toBottomOf="@+id/image"
+ app:layout_constraintVertical_bias="0.0" />
\ No newline at end of file