2016-05-10 23 views
0

私は、MediaProjectionクラスで画像をキャプチャするためのテストアプリケーションを作成しました。オリエンテーションを変更してMediaProjectionで画像キャプチャを処理する方法は?

Bitmap bitmap; 

Image mImage = null; 

try { 

mImage = mImageReader .acquireLatestImage(); 

if (img != null) { 

    final Image.Plane[] planes = mImage.getPlanes(); 

    final ByteBuffer buffer = planes[0].getBuffer(); 

    int pixelStride = planes[0].getPixelStride(); 

    int rowStride = planes[0].getRowStride(); 

    int rowPadding = rowStride - pixelStride * mImage.getWidth(); 

    bitmap = Bitmap.createBitmap(mImage.getWidth() + rowPadding/pixelStride, mImage.getHeight(), Bitmap.Config.ARGB_8888); 

    bitmap.copyPixelsFromBuffer(buffer); 

    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 

    byte[] rawData = lastImageAcquiredRaw = stream.toByteArray(); 

    if (rawData != null) { 

    Bitmap fullScreen = BitmapFactory.decodeByteArray(rawData, 0, rawData.length); 

    savebitmap(fullScreen,"image",i); //Saving bitmap in storage 

    } 

} 

今、私はOKだと私はティル:

imageReader = ImageReader.newInstance(currentWidth, currentHeight, PixelFormat.RGBA_8888, 2); 
imageReader.setOnImageAvailableListener(this, null); 

virtualDisplay = mediaProjection.createVirtualDisplay("captureDisplay", 
        currentWidth, currentHeight, DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | 
          DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC| 
          DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR | 
          DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE | 
          DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION, Screen.getDensity(mContext),       imageReader.getSurface(), null, null); 

// DISPLAYMANAGERフラグが唯一

onImageAvailable(ImageReader reader)方法

に、私は次のように画像を取得しようとしましたトレイルです私のアプリが横向きのときに正しいImageを取得する。直面している問題は向きが変わっている、私は適切な画像を取得していない。景色に戻って再び適切にキャプチャしていない時に再び何度か。

私はImageReader.javaクラスを使いました。オリエンテーションの変更を処理する必要性について何も言及していません。

acquireNextImageNoThrowISE()、acquireNextImage()を使用してみましたが、使用しませんでした。

オリエンテーションで正しい画像を取得しようとしたことがありますか?

適切な画像を取得するのに手伝ってください。

よろしく

答えて

1

私はこの記事が少し古いですけど、日の最後のカップルは、私は同じことを絞めています。 MediaProjection API、VirtualDisplay、ImageReaderのサーフェイスも使用しています。私が実証しようとしている解決策は100%完全保証ではないので、誰かが有益な提案をしているのであれば歓迎すべきものではありません。

ステップ1:あなたは仮想画面を作成した後OrientationChangeCallbackを追加

OrientationChangeCallback orientationChangeCallback = new OrientationChangeCallback(context); 
if (orientationChangeCallback.canDetectOrientation()) { 
    orientationChangeCallback.enable(); 
} 

ステップ2:本質的には、キャプチャを再起動する姿勢コールバックを定義

private class OrientationChangeCallback extends OrientationEventListener { 
     OrientationChangeCallback(Context context) { 
      super(context); 
     } 

     @Override 
     public void onOrientationChanged(int orientation) { 
      final int rotation = mDisplay.getRotation(); 
      if(rotation != mRotation) { 
       mRotation = rotation; 
       if (mVirtualDisplay != null) { 
        mVirtualDisplay.release(); 
       } 
       if (mImageReader != null) { 
        mImageReader.setOnImageAvailableListener(null, null); 
       } 
       if (!mProjectionStopped) { 
        createVirtualDisplay(); 
       } 
      } 
     } 
    } 

なお、I ImageReaderとVirtualDisplayへの参照を保持していて、画面が実際に回転したかどうかを確認するにはvolatile boolean mRotationがあります。

問題は、一部のデバイス(Nexus 5Xを含む)では、X番号または回転(通常は10-20)後に画像が壊れることがあるということです。もう一度デバイスを回転させると、イメージが正しくキャプチャされます。クイックデバイス(Google Pixel)では、壊れた画像の問題を再現することができないため、同期の問題であると推測しています。

+0

「canDetectOrientation」とは何ですか? – 1234567

+0

完全なコードを共有できますか?https://github.com/mtsahakis/MediaProjectionDemo – mtsahakis

関連する問題