How can i take/merge screen shot of Google map v2 and layout of xml both programmatically?

你说的曾经没有我的故事 提交于 2020-01-19 05:04:05

问题


I am taking screenshot of Google Map v2 by using code of this answer which is giving me output :

Which is fine to take screen shot of Map

With following code i can take the screen shot of Layout with black map screen thats ok as with following code Map will black in ScreenShot

String mPath = Environment.getExternalStorageDirectory().toString()
                + "/" + "myTestScr" + System.currentTimeMillis() + ".jpeg";
Bitmap bitmap;
        View v1 = (View) findViewById(R.id.rootviewtest);
        v1.setDrawingCacheEnabled(true);
        bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

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

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

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

output of above code:

What I actually need is:

So, Now My question that how can get the output like in 3rd Screen programmatically?

Help me to take one screen shot by merging both (1 and 2) screens programmatically?

or any other alternate to merge both images programmatically after taking both ( 1 and 2 ) screen shots ?


回答1:


Call the following method to take the screenshot with map:

public void captureMapScreen() {
        SnapshotReadyCallback callback = new SnapshotReadyCallback() {

            @Override
            public void onSnapshotReady(Bitmap snapshot) {
                try {
                    mView.setDrawingCacheEnabled(true);
                    Bitmap backBitmap = mView.getDrawingCache();
                    Bitmap bmOverlay = Bitmap.createBitmap(
                            backBitmap.getWidth(), backBitmap.getHeight(),
                            backBitmap.getConfig());
                    Canvas canvas = new Canvas(bmOverlay);
                    canvas.drawBitmap(snapshot, new Matrix(), null);
                    canvas.drawBitmap(backBitmap, 0, 0, null);
                    FileOutputStream out = new FileOutputStream(
                            Environment.getExternalStorageDirectory()
                                    + "/MapScreenShot"
                                    + System.currentTimeMillis() + ".png");

                    bmOverlay.compress(Bitmap.CompressFormat.PNG, 90, out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        mMap.snapshot(callback);

    }

mview is the root view of your layout and mMap is your map fragment.

Make sure that you have the latest Google Play Services API.

mView.setDrawingCacheEnabled(true);
Bitmap backBitmap = mView.getDrawingCache();
Bitmap bmOverlay = Bitmap.createBitmap(
backBitmap.getWidth(), backBitmap.getHeight(),
backBitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(snapshot, new Matrix(), null);
canvas.drawBitmap(backBitmap, 0, 0, null);

Skip these lines and use snapshot.compress(Bitmap.CompressFormat.PNG, 90, out); if you want the screenshot of map only.




回答2:


I don't know Android API, but since you requested "alternate" methods I'll suggest using ImageMagick. On that site there are several binaries, thus I hope you can download one that suits your system. Otherwise you can download the sources (google around and you'll find many links about building it for Android).

Since I work on a Windows machine I can only tell you how you can invoke it from Windows command-line, but the syntax is similar for other platforms. This app has also a vast selection of APIs for different languages (Java, C or C++ may be of interest for you in particular), so that you can use ImageMagick embedded in your application instead of invoking it from the command line.

Now for the problem at hand: the operation you need to do is to put the "overlay sheet" with symbols over your map, like you'd do with a transparent sheet with drawings over a real map.

So let's call map.png and overlay.png the two files to be merged. Note that also the overlay file is a PNG file (more on this later). Then you can obtain something close to what you desire with the following command-line invocation:

composite.exe -compose atop overlay.png map.png output.jpg

Why must the overlay also be a PNG image? That's because we need an "overlay sheet" that is transparent in most areas (except where you have the balloon tip and the other drawings), but JPG images don't store the transparency information (they don't have the alpha-channel, which stores this information).

So you should modify the part of your code that generates the overlay so as to generate a PNG image with an appropriate alpha channel.

Hope all this helps.



来源:https://stackoverflow.com/questions/18309625/how-can-i-take-merge-screen-shot-of-google-map-v2-and-layout-of-xml-both-program

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