taking screenshot camera view+layout view android augmented reality

你说的曾经没有我的故事 提交于 2019-12-11 02:39:26

问题


I tried taking screencapture from frame buffer and it works well for my layout views I used the following code from here:

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

But I want to capture layout view with camera view, since I am using cameraview in background of layout views or transparent webpage as well. One turnaround is to take camera image and layout view separately and put them on each other and save new bitmap.

Can anyone suggest me any proper solution to this? Thanks in advance


回答1:


in order to put two images together you may like to follow this code its working 100% to overlay two images together

 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

now part to save the view , the easy way is to

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

and then :

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

this will save you the view of the imageview , or if its Layout then just set your layout and give it a name and then replace it with view and please do not forget after u check whether the saving succeed or not to set

        view.setDrawingCacheEnabled(false); 



回答2:


i couldn't post the code as comment so here you go will you can pass that specific method on your onActivtity or just pass it after u send the intent , or of its custom camera then you can control that when you click on the camera button just add a method to capture the view. this is my method for saving the view.

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

in order for you to save the view continually then just add loop . when you click the camera button that loop starts !! and here in my saving method , don't worry about overlaying names :). randomly it will generate the names for each image!



来源:https://stackoverflow.com/questions/13565221/taking-screenshot-camera-viewlayout-view-android-augmented-reality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!