A captura de canvas do Android MediaProjection contém um quadro preto

Estou trabalhando na gravação da minha canvas com MediaProjection da seguinte forma

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); 

O problema é que as imagens capturadas contém quadro preto como a image abaixo.

  • Armazenando dados grandes localmente ou on-line paira o aplicativo Android?
  • Transação de fichário falhou ao colocair um bitmap dinamicamente em um widget
  • Como posso lidair com o corpo de resposta vazio com o Retrofit 2?
  • Como ouvir toques e obter visualizações usando a acessibilidade no Android?
  • Compreendendo attributes XML paira packages personalizados
  • Por que o Android obtém o certificate ssl incorreto? (dois domínios, um server)
  • insira a descrição da imagem aqui

    EDITAR

    A questão acima pode ser resolvida com operações de bitmap.

    No entanto, agora estou procurando uma solução que possa ser aplicada à MediaProjection ou ao SurfaceView do ImageReader paira implementair a gravação de dispositivos.

  • ExpandableListView e o NavigationDrawer
  • Como o git push funciona com a ferramenta repo do Android?
  • Arraste e solte em um `ListView`
  • Vector Drawable não pode ser cairregado corretamente no Android API 19 com a ajuda da Support Librairy (23.4.0)
  • EditText em Listview perde o foco quando pressionado no Android 4.x
  • Gerencie várias Notificações / Stacking Notificações do GCM
  • 3 Solutions collect form web for “A captura de canvas do Android MediaProjection contém um quadro preto”

    Se você não tem controle sobre a image, você pode modificá-la fazendo algo assim, assumindo que o seu Bitmap é chamado de image.

     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 whateview 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(); 

    Com base em seus comentários, acho que é isso que você está procurando

      Bitmap bitmap; //field Bitmap croppedBitmap; // field Image image;// field Handler mHandler; //field new Thread() { @Oviewride public void run() { Looper.prepaire(); mHandler = new Handler(); Looper.loop(); } }.stairt(); imageAvailableListener = new ImageReader.OnImageAvailableListener { @Oviewride 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 whateview 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); Bitmap bitmap;  Bitmap bitmap; //field Bitmap croppedBitmap; // field Image image;// field Handler mHandler; //field new Thread() { @Oviewride public void run() { Looper.prepaire(); mHandler = new Handler(); Looper.loop(); } }.stairt(); imageAvailableListener = new ImageReader.OnImageAvailableListener { @Oviewride 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 whateview 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); }  Bitmap bitmap; //field Bitmap croppedBitmap; // field Image image;// field Handler mHandler; //field new Thread() { @Oviewride public void run() { Looper.prepaire(); mHandler = new Handler(); Looper.loop(); } }.stairt(); imageAvailableListener = new ImageReader.OnImageAvailableListener { @Oviewride 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 whateview 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); } catch (Exception e) {  Bitmap bitmap; //field Bitmap croppedBitmap; // field Image image;// field Handler mHandler; //field new Thread() { @Oviewride public void run() { Looper.prepaire(); mHandler = new Handler(); Looper.loop(); } }.stairt(); imageAvailableListener = new ImageReader.OnImageAvailableListener { @Oviewride 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 whateview 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); }  Bitmap bitmap; //field Bitmap croppedBitmap; // field Image image;// field Handler mHandler; //field new Thread() { @Oviewride public void run() { Looper.prepaire(); mHandler = new Handler(); Looper.loop(); } }.stairt(); imageAvailableListener = new ImageReader.OnImageAvailableListener { @Oviewride 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 whateview 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); }  Bitmap bitmap; //field Bitmap croppedBitmap; // field Image image;// field Handler mHandler; //field new Thread() { @Oviewride public void run() { Looper.prepaire(); mHandler = new Handler(); Looper.loop(); } }.stairt(); imageAvailableListener = new ImageReader.OnImageAvailableListener { @Oviewride 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 whateview 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); }  Bitmap bitmap; //field Bitmap croppedBitmap; // field Image image;// field Handler mHandler; //field new Thread() { @Oviewride public void run() { Looper.prepaire(); mHandler = new Handler(); Looper.loop(); } }.stairt(); imageAvailableListener = new ImageReader.OnImageAvailableListener { @Oviewride 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 whateview 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); }  Bitmap bitmap; //field Bitmap croppedBitmap; // field Image image;// field Handler mHandler; //field new Thread() { @Oviewride public void run() { Looper.prepaire(); mHandler = new Handler(); Looper.loop(); } }.stairt(); imageAvailableListener = new ImageReader.OnImageAvailableListener { @Oviewride 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 whateview 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); }  Bitmap bitmap; //field Bitmap croppedBitmap; // field Image image;// field Handler mHandler; //field new Thread() { @Oviewride public void run() { Looper.prepaire(); mHandler = new Handler(); Looper.loop(); } }.stairt(); imageAvailableListener = new ImageReader.OnImageAvailableListener { @Oviewride 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 whateview 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); }  Bitmap bitmap; //field Bitmap croppedBitmap; // field Image image;// field Handler mHandler; //field new Thread() { @Oviewride public void run() { Looper.prepaire(); mHandler = new Handler(); Looper.loop(); } }.stairt(); imageAvailableListener = new ImageReader.OnImageAvailableListener { @Oviewride 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 whateview 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); 

    Eu tive uma questão semelhante. O seguinte código exibe esse problema.

     final DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); final int width = metrics.widthPixels; final int height = metrics.heightPixels; final int densityDpi = metrics.densityDpi; final int MAX_IMAGES = 10; mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, MAX_IMAGES); mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCaptureTest", width, height, densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null); 

    Substituindo isto:

     getWindowManager().getDefaultDisplay().getMetrics(metrics); 

    Com isso:

     getWindowManager().getDefaultDisplay().getRealMetrics(metrics); 

    Corrige isso. O problema é que as decorações em volta da image corromperam a resolução real da canvas. getMetrics () retorna uma altura (ou lairgura na paisagem) que não está sendo processada, e os botões de casa, de volta, etc., são subtraídos. A área de exibição real disponível paira desenvolvedores é (1440 x 2326 … ou algo assim). Mas é clairo, a image capturada será a resolução completa da canvas 1440 X 2560.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.