簡體   English   中英

在后台服務中截屏

[英]Taking screenshot in a Background Service

我正在嘗試制作一個可以截取其他應用程序的氣泡應用程序。

我找到了這個帶有媒體投影示例的項目鏈接,但圖像沒有保存到設備中。

有沒有辦法可以將此圖像保存到設備上,或者有沒有其他方法可以在后台服務中使用我的應用程序截取屏幕截圖。

我已經檢查了鏈接,我認為有一種方法可以保存從屏幕截圖中獲取的文件。

來自ImageTransmogrifier class

@Override
  public void onImageAvailable(ImageReader reader) {
    final Image image=imageReader.acquireLatestImage();

    if (image!=null) {
      Image.Plane[] planes=image.getPlanes();
      ByteBuffer buffer=planes[0].getBuffer();
      int pixelStride=planes[0].getPixelStride();
      int rowStride=planes[0].getRowStride();
      int rowPadding=rowStride - pixelStride * width;
      int bitmapWidth=width + rowPadding / pixelStride;

      if (latestBitmap == null ||
          latestBitmap.getWidth() != bitmapWidth ||
          latestBitmap.getHeight() != height) {
        if (latestBitmap != null) {
          latestBitmap.recycle();
        }

        latestBitmap=Bitmap.createBitmap(bitmapWidth,
            height, Bitmap.Config.ARGB_8888);
      }

      latestBitmap.copyPixelsFromBuffer(buffer);
      image.close();

      ByteArrayOutputStream baos=new ByteArrayOutputStream();
      Bitmap cropped=Bitmap.createBitmap(latestBitmap, 0, 0,
        width, height);

      cropped.compress(Bitmap.CompressFormat.PNG, 100, baos);

      byte[] newPng=baos.toByteArray();

      svc.processImage(newPng);
    }
  }

如您所見,那里有一個latestBitmap object,從這里您實際上可以將此 bitmap 保存到您要保存的任何位置。

例如,您可以參考此鏈接https://www.simplifiedcoding.net/android-save-bitmap-to-gallery/將其保存在您的圖庫中:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM