11

次のように、MediaProjection を使用して画面を記録する作業を行っています。

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
displayWidth = size.x;
displayHeight = size.y;

imageReader = ImageReader.newInstance(displayWidth, displayHeight, ImageFormat.JPEG, 5);

int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;

DisplayMetrics metrics = getResources().getDisplayMetrics();
int density = metrics.densityDpi;

mediaProjection.createVirtualDisplay("test", displayWidth, displayHeight, density, flags, 
      imageReader.getSurface(), null, projectionHandler);

Image image = imageReader.acquireLatestImage();
byte[] data = getDataFromImage(image);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

問題は、キャプチャされた画像に下の画像のような黒いフレームが含まれることです。

ここに画像の説明を入力

編集

上記の問題は、ビットマップ操作で解決できます。

ただし、デバイスの記録を実装するMediaProjectionためSurfaceViewに適用できるソリューションを探しています。ImageReader

4

3 に答える 3

4

自分で画像を制御できない場合は、ビットマップが画像と呼ばれると仮定して、次のようにして画像を変更できます。

Bitmap imageWithBG = Bitmap.createBitmap(image.getWidth(), image.getHeight(),image.getConfig());  // Create another image the same size
imageWithBG.eraseColor(Color.BLACK);  // set its background to white, or whatever color you want
Canvas canvas = new Canvas(imageWithBG);  // create a canvas to draw on the new image
canvas.drawBitmap(image, 0f, 0f, null); // draw old image on the background
image.recycle(); 
于 2016-01-21T05:56:09.443 に答える
1

あなたのコメントに基づいて、これがあなたが探しているものだと思います

    Bitmap bitmap; //field
    Bitmap  croppedBitmap; // field
    Image image;// field
    Handler mHandler; //field


    new Thread() {
        @Override
        public void run() {
            Looper.prepare();
            mHandler = new Handler();
            Looper.loop();
        }
    }.start();

    imageAvailableListener = new ImageReader.OnImageAvailableListener {
    @Override
    public void onImageAvailable(ImageReader reader) {

        try {
            image = reader.acquireLatestImage();
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            Rect rect = image.getCropRect();
            croppedBitmap =  Bitmap.createBitmap(bitmap,rect.left,rect.top,rect.width(),rect.height());

            \\Do whatever here...


            image.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

               if (bitmap!=null) {
                   bitmap.recycle();
               }

              if (croppedBitmap!=null) {
                   bitmap.recycle();
               }

              if (image!=null) {
                   image.close();
               }
           }
        }
    }
    imageReader.setOnImageAvailableListener(imageAvailableListener, mHandler);
于 2016-01-19T21:34:50.410 に答える