简体   繁体   English

在android中以编程方式截取屏幕截图

[英]Taking screenshot programmatically in android

I am taking screenshot programmatically using the following code:我正在使用以下代码以编程方式截取屏幕截图:

public static Bitmap takeScreenshot(View view)
    {
        try
        {
            // create bitmap screen capture
            view.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);
            return bitmap;
        }
        catch (Throwable e)
        {
            CustomLogHandler.printError(e);
        }
        return null;
    }

private static void copyFile(Bitmap bitmap)
    {
        File dstFile = getShareResultFile();

        //Delete old file if exist.
        if(dstFile.exists()) {
            dstFile.delete();
        }

        FileOutputStream fos = null;
        try
        {
            fos = new FileOutputStream(dstFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 0, fos);
            fos.flush();
        }
        catch (Exception e) {
            CustomLogHandler.printError(e);
        }
        finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ioe) {
                    CustomLogHandler.printError(ioe);
                }
            }
        }
    }

There are several problem like:有几个问题,如:

  1. Back arrow, title and share menu background color is not correct.后退箭头、标题和共享菜单背景颜色不正确。 It looks messy.看起来很乱。
  2. Background color of toolbar is totally changed.工具栏的背景颜色完全改变。
  3. Image quality is too poor and list items rounded drawable has not smooth corners.图像质量太差,可绘制的列表项圆角不平滑。
  4. Background of layout is not taken that I set as background of my parent layout.未采用我设置为父布局背景的布局背景。

I am taking the screenshot from the root view.我正在从根视图截取屏幕截图。

嘛

bitmap.compress(Bitmap.CompressFormat.JPEG, 0, fos);

First, you are saving this as a JPEG.首先,您将其保存为 JPEG。 JPEG is designed for photos, and your screenshot is not a photo. JPEG 专为照片而设计,您的屏幕截图不是照片。

Second, you are saving this with a quality factor of 0. JPEG uses a lossy compression algorithm, and a quality factor of 0 says "please feel free to make this image be really poor, but compress it as far as you can".其次,您使用质量因子 0 保存它。JPEG 使用有损压缩算法,质量因子 0 表示“请随意使该图像非常差,但尽可能压缩它”。

I suggest switching to:我建议切换到:

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

PNG is a better image format for a screenshot with the contents shown in your question. PNG 是一种更好的图像格式,可用于截取您的问题中显示的内容。 I don't think PNG uses the quality factor value;我不认为 PNG 使用质量因子值; I put in 100 just to indicate that you want the best possible quality.我输入 100 只是为了表明您想要最好的质量。

public static Bitmap takeScreenshot(View view)
{
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

This code can save view as bitmap.此代码可以将视图保存为位图。 But after you update your question with save code I see that you set 0 for quality, and what you expect?但是在您使用保存代码更新您的问题后,我看到您将质量设置为 0,您期望什么?

@param quality  Hint to the compressor, 0-100. 0 meaning compress for
     *                 small size, 100 meaning compress for max quality. Some
     *                 formats, like PNG which is lossless, will ignore the
     *                 quality setting

just use your Ctrl button + click on method name to read doc about params只需使用您的 Ctrl 按钮 + 单击方法名称即可阅读有关参数的文档

the answer is set second parameter 100 instead of 0!答案是设置第二个参数 100 而不是 0!

Try using this:尝试使用这个:

  public static Bitmap loadBitmapFromView(Context context, View v) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics(); 
        v.measure(MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.EXACTLY));
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
        Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
                v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(returnedBitmap);
        v.draw(c);

        return returnedBitmap;
    }

and

public void takeScreen() {
    Bitmap bitmap = ImageUtils.loadBitmapFromView(this, view); //get Bitmap from the view
    String mPath = Environment.getExternalStorageDirectory() + File.separator + "screen_" + System.currentTimeMillis() + ".jpeg";
    File imageFile = new File(mPath);
    OutputStream fout = null;
    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
        fout.flush();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        fout.close();
    }
}

Images are saved in the external storage folder.图像保存在外部存储文件夹中。

try this尝试这个

private void captureScreen() {
     View v =  this.getWindow().getDecorView().findViewById(android.R.id.content);
    v.setDrawingCacheEnabled(true);
    Bitmap bitmap = v.getDrawingCache();
    String extr = Environment.getExternalStorageDirectory().toString();
    File file = new File(extr, getString(R.string.free_tiket) + ".jpg");
    FileOutputStream f = null;
    try {
        f = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, f);
        f.flush();
        f.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Screen", "screen");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM