2

我尝试从帧缓冲区获取屏幕截图,它适用于我的布局视图,我从这里使用了以下代码:

String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

但我想用相机视图捕捉布局视图,因为我也在布局视图或透明网页的背景中使用相机视图。一种解决方法是分别拍摄相机图像和布局视图并将它们放在一起并保存新的位图。

谁能建议我对此有任何适当的解决方案?提前致谢

4

2 回答 2

0

为了将两个图像放在一起,您可能希望按照此代码 100% 工作以将两个图像叠加在一起

 Bitmap border = BitmapFactory.decodeResource(getResources(), R.drawable.vignette2);
int width = bmp.getWidth();
int height = bmp.getHeight();
change = Bitmap.createScaledBitmap(change, width, height, false); // change is Bitmap
Canvas canvas = new Canvas(change);
Bitmap scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
canvas.drawBitmap(scaledBorder, 0, 0,null);
//canvas.drawBitmap(k, 0, 0, null);
view.setImageBitmap(change); // view is the imageView

现在部分保存视图,简单的方法是

view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        save = view.getDrawingCache(); // save is a Bitmap

接着 :

                final FileOutputStream out = new FileOutputStream(file);
            save.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));
            Toast.makeText(getApplication(), "Image Saved",
                    Toast.LENGTH_SHORT).show();

这将为您保存图像视图的视图,或者如果它的布局则只需设置您的布局并给它一个名称然后替换它,view 请不要忘记在您检查保存是否成功设置后

        view.setDrawingCacheEnabled(false); 
于 2013-02-09T12:55:54.927 回答
0

我无法将代码作为评论发布,所以你可以在你的 onActivtity 上传递该特定方法,或者在你发送意图后传递它,或者它的自定义相机,然后你可以控制当你点击相机按钮时只需添加一个方法来捕获视图。这是我保存视图的方法。

void Save() {
    if (null != view.getDrawable()) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        save = view.getDrawingCache();
        final File myDir = new File(folder);
        myDir.mkdirs();
        final Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        final String fname = "StyleMe-" + n + ".png";
        final File file = new File(myDir, fname);
        if (file.exists())
            file.delete();
        try {
            final FileOutputStream out = new FileOutputStream(file);
            save.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));
            Toast.makeText(getApplication(), "Image Saved",
                    Toast.LENGTH_SHORT).show();
        } catch (final Exception e) {
            Toast.makeText(getApplication(),
                    "Something Went Wrong check if you have Enough Memory",
                    Toast.LENGTH_LONG).show();
        }
    } else {
        final Toast tst = Toast.makeText(getApplication(),
                "Please Select An Image First", Toast.LENGTH_LONG);
        tst.setGravity(Gravity.CENTER, 0, 0);
        tst.show();
    }
    view.setDrawingCacheEnabled(false);
}

为了让您不断保存视图,然后只需添加循环。当您单击循环开始的相机按钮时!在我的保存方法中,不要担心覆盖名称:)。它将随机生成每个图像的名称!

于 2013-02-09T13:06:56.763 回答