簡體   English   中英

截圖時為黑色圖像

[英]Black Image when taking screenshot

我想使用位圖捕獲Android屏幕,然后將其轉換為圖像。 該方法應該是靜態的。

這是在我的代碼中注冊的按鈕單擊,單擊時,“ takeScrernshot”將捕獲TextureView並將其轉換為Bitmap,然后將其保存到File

public class MainActivity extends AppCompatActivity {

    //  View rootView = getWindow().getDecorView().getRootView();
    private static TextureView textureView;
    Button button;
    SurfaceView mPreview;
    private SurfaceHolder holder;

    private static File screenshotFile;

    private static float videoWidth = 1080.0f;
    private static float videoHeight = 1920.0f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textureView = (TextureView) findViewById(R.id.textureView);
        button = (Button) findViewById(R.id.button);

       // mPreview = (SurfaceView) findViewById(R.id.surface);
//        holder = mPreview.getHolder();
       // holder.setFormat(PixelFormat.RGBX_8888);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // takeScreenShot();
                takeScreenshotVideoPlaying();
            }
        });
    }



    public static Bitmap takeScreenshotVideoPlaying() {
        Log.d("", "takeScreenshotVideoPlaying() in VideoPlayerActivity running!");

        Bitmap bitmap = null;
        int tries = 100;
        for (int i = 1; i < tries; i++) {
            if (textureView.isAvailable()) {

                float bitmapScale = 4.0f;
                float bitmapWidth = videoWidth / bitmapScale;
                float bitmapHeight = videoHeight / bitmapScale;
                bitmap = textureView.getBitmap((int) bitmapWidth, (int) bitmapHeight);
                if (bitmap != null) {
                    Log.d("", "screenshot taken");
                    break;
                }
            }
        }
        saveScreenshot(bitmap);
        return bitmap;
    }


    private static void saveScreenshot(Bitmap bitmap) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();

        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);


        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
        // see if this helps the uploading to GCS

        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        screenshotFile = new File(mPath);
        try {
            if (screenshotFile.exists()) {
                screenshotFile.delete();
            }
            if (screenshotFile.createNewFile()) {
                FileOutputStream fo = new FileOutputStream(screenshotFile);
                fo.write(bytes.toByteArray());
                fo.close();
                Log.d("Saved", "saveScreenshot: ");

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}



**and this is how layout looks like:**

我有一個紋理視圖,需要注冊水龍頭,然后單擊按鈕以創建屏幕快照(僅用於測試)將位圖轉換為文件后-圖像為黑色

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#534783"
    tools:context="com.maks.testscreenshots.MainActivity">


    <TextureView
        android:id="@+id/textureView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_dark"/>

    <!--<SurfaceView-->
        <!--android:id="@+id/surface"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_gravity="center" />-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="Button"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:background="@color/colorAccent"
        android:textSize="100sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/textView" />

</android.support.constraint.ConstraintLayout>

問題是,我一直在努力截圖textureView ,其渲染的視頻,所以,當我試圖getBitmap屏幕,我得到的黑色屏幕,由於圖像每次都改變。 要捕獲視頻,我使用了兩種不同的方法:-對於有根設備,我使用鼠標光標或屏幕上的任何其他東西捕獲了整個屏幕:

 private static Bitmap takeScreenshot() {
        Bitmap screen;
        Bitmap resizedBitmap = null;
        try {
            Process sh = Runtime.getRuntime().exec("su", null, null);

            OutputStream os = sh.getOutputStream();
            Log.d("SCREENSHOT ", "PATH: " + ActFileUtil.getPathToScreenshotDirOnDevice());
            os.write(("/system/bin/screencap -p " + ActFileUtil.getPathToScreenshotDirOnDevice()).getBytes("ASCII"));
            os.flush();
            os.close();
            sh.waitFor();
            Log.d(TAG, "procScreenshot: SCREENSHOT TAKEN");
            screen = BitmapFactory.decodeFile(ActFileUtil.getPathToScreenshotDirOnDevice());
            if(screen != null) {
                resizedBitmap = Bitmap.createScaledBitmap(screen, screen.getWidth() / 4, screen.getHeight() / 4, true);
            }


        } catch (IOException | NullPointerException | InterruptedException e) {
            e.printStackTrace();
        }

        return resizedBitmap;
    }
  • 對於沒有root用戶的設備,我只使用了MediaPlayer內置method

    位圖bm = mMediaPlayer.getCurrentFrame();

暫無
暫無

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

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