[android] map snapshotter - wrap snapshot in native peer for access to the pointFor functionality

This commit is contained in:
Ivo van Dongen
2017-10-09 16:54:32 +03:00
committed by Ivo van Dongen
parent 87b735aaf7
commit dde1a92291
14 changed files with 293 additions and 28 deletions

View File

@@ -0,0 +1,45 @@
package com.mapbox.mapboxsdk.snapshotter;
import android.graphics.Bitmap;
import android.graphics.PointF;
import com.mapbox.mapboxsdk.geometry.LatLng;
/**
* A completed snapshot.
*
* @see MapSnapshotter
*/
public class MapSnapshot {
private long nativePtr = 0;
private Bitmap bitmap;
/**
* Created from native side
*/
private MapSnapshot(long nativePtr, Bitmap bitmap) {
this.nativePtr = nativePtr;
this.bitmap = bitmap;
}
/**
* @return the bitmap
*/
public Bitmap getBitmap() {
return bitmap;
}
/**
* Calculate the point in pixels on the Image from geographical coordinates.
*
* @param latLng the geographical coordinates
* @return the point on the image
*/
public native PointF pixelForLatLng(LatLng latLng);
// Unused, needed for peer binding
private native void initialize();
protected native void finalize();
}

View File

@@ -12,7 +12,6 @@ import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.storage.FileSource;
/**
@@ -23,11 +22,27 @@ import com.mapbox.mapboxsdk.storage.FileSource;
@UiThread
public class MapSnapshotter {
/**
* Get notified on snapshot completion.
*
* @see MapSnapshotter#start(SnapshotReadyCallback, ErrorHandler)
*/
public interface SnapshotReadyCallback {
/**
* Called when the snapshot is complete.
*
* @param snapshot the snapshot
*/
void onSnapshotReady(MapSnapshot snapshot);
}
/**
* Can be used to get notified of errors
* in snapshot generation
*
* @see MapSnapshotter#start(MapboxMap.SnapshotReadyCallback, ErrorHandler)
* @see MapSnapshotter#start(SnapshotReadyCallback, ErrorHandler)
*/
public interface ErrorHandler {
@@ -46,7 +61,7 @@ public class MapSnapshotter {
private long nativePtr = 0;
private final Context context;
private MapboxMap.SnapshotReadyCallback callback;
private SnapshotReadyCallback callback;
private ErrorHandler errorHandler;
/**
@@ -176,7 +191,7 @@ public class MapSnapshotter {
*
* @param callback the callback to use when the snapshot is ready
*/
public void start(@NonNull MapboxMap.SnapshotReadyCallback callback) {
public void start(@NonNull SnapshotReadyCallback callback) {
this.start(callback, null);
}
@@ -187,7 +202,7 @@ public class MapSnapshotter {
* @param callback the callback to use when the snapshot is ready
* @param errorHandler the error handler to use on snapshot errors
*/
public void start(@NonNull MapboxMap.SnapshotReadyCallback callback, ErrorHandler errorHandler) {
public void start(@NonNull SnapshotReadyCallback callback, ErrorHandler errorHandler) {
if (this.callback != null) {
throw new IllegalStateException("Snapshotter was already started");
}
@@ -247,12 +262,12 @@ public class MapSnapshotter {
* Called by JNI peer when snapshot is ready.
* Always called on the origin (main) thread.
*
* @param bitmap the generated snapshot
* @param snapshot the generated snapshot
*/
protected void onSnapshotReady(Bitmap bitmap) {
protected void onSnapshotReady(MapSnapshot snapshot) {
if (callback != null) {
addOverlay(bitmap);
callback.onSnapshotReady(bitmap);
addOverlay(snapshot.getBitmap());
callback.onSnapshotReady(snapshot);
reset();
}
}

View File

@@ -387,6 +387,16 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity android:name=".activity.snapshot.MapSnapshotterMarkerActivity"
android:description="@string/description_map_snapshotter_marker"
android:label="@string/activity_map_snapshotter_marker">
<meta-data
android:name="@string/category"
android:value="@string/category_imagegenerator"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity
android:name=".activity.maplayout.DoubleMapActivity"
android:description="@string/description_doublemap"

View File

@@ -1,6 +1,5 @@
package com.mapbox.mapboxsdk.testapp.activity.snapshot;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewTreeObserver;
@@ -11,7 +10,7 @@ import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshot;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter;
import com.mapbox.mapboxsdk.testapp.R;
@@ -96,12 +95,12 @@ public class MapSnapshotterActivity extends AppCompatActivity {
MapSnapshotter snapshotter = new MapSnapshotter(MapSnapshotterActivity.this, options);
snapshotter.start(new MapboxMap.SnapshotReadyCallback() {
snapshotter.start(new MapSnapshotter.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap snapshot) {
public void onSnapshotReady(MapSnapshot snapshot) {
Timber.i("Got the snapshot");
ImageView imageView = new ImageView(MapSnapshotterActivity.this);
imageView.setImageBitmap(snapshot);
imageView.setImageBitmap(snapshot.getBitmap());
grid.addView(
imageView,
new GridLayout.LayoutParams(GridLayout.spec(row), GridLayout.spec(column))

View File

@@ -0,0 +1,77 @@
package com.mapbox.mapboxsdk.testapp.activity.snapshot;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshot;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter;
import com.mapbox.mapboxsdk.testapp.R;
import timber.log.Timber;
/**
* Test activity showing how to use a the {@link MapSnapshotter} and overlay
* {@link android.graphics.Bitmap}s on top.
*/
public class MapSnapshotterMarkerActivity extends AppCompatActivity implements MapSnapshotter.SnapshotReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_snapshotter_marker);
final View container = findViewById(R.id.container);
container.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//noinspection deprecation
container.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Timber.i("Starting snapshot");
MapSnapshotter mapSnapshotter = new MapSnapshotter(
getApplicationContext(),
new MapSnapshotter
.Options(Math.min(container.getMeasuredWidth(), 1024), Math.min(container.getMeasuredHeight(), 1024))
.withStyle(Style.TRAFFIC_DAY)
.withCameraPosition(new CameraPosition.Builder().target(new LatLng(52.090737, 5.121420)).zoom(15).build())
);
mapSnapshotter.start(MapSnapshotterMarkerActivity.this);
}
});
}
@Override
public void onSnapshotReady(MapSnapshot snapshot) {
Timber.i("Snapshot ready");
ImageView imageView = (ImageView) findViewById(R.id.snapshot_image);
Bitmap image = addMarker(snapshot);
imageView.setImageBitmap(image);
}
private Bitmap addMarker(MapSnapshot snapshot) {
Canvas canvas = new Canvas(snapshot.getBitmap());
Bitmap marker = BitmapFactory.decodeResource(getResources(), R.drawable.mapbox_marker_icon_default, null);
// Dom toren
PointF markerLocation = snapshot.pixelForLatLng(new LatLng(52.090649433011315, 5.121310651302338));
canvas.drawBitmap(marker,
markerLocation.x,
/* Subtract height (in dp) so the bottom of the marker aligns correctly */
markerLocation.y - (marker.getHeight() / getResources().getDisplayMetrics().density),
null
);
return snapshot.getBitmap();
}
}

View File

@@ -1,6 +1,5 @@
package com.mapbox.mapboxsdk.testapp.activity.snapshot;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
@@ -10,7 +9,7 @@ import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshot;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter;
import com.mapbox.mapboxsdk.testapp.R;
@@ -19,7 +18,7 @@ import java.util.Random;
/**
* Test activity showing how to use a the {@link MapSnapshotter}
*/
public class MapSnapshotterReuseActivity extends AppCompatActivity implements MapboxMap.SnapshotReadyCallback {
public class MapSnapshotterReuseActivity extends AppCompatActivity implements MapSnapshotter.SnapshotReadyCallback {
private MapSnapshotter mapSnapshotter;
private View fab;
@@ -61,10 +60,10 @@ public class MapSnapshotterReuseActivity extends AppCompatActivity implements Ma
}
@Override
public void onSnapshotReady(Bitmap snapshot) {
public void onSnapshotReady(MapSnapshot snapshot) {
fab.setVisibility(View.VISIBLE);
ImageView imageView = (ImageView) findViewById(R.id.snapshot_image);
imageView.setImageBitmap(snapshot);
imageView.setImageBitmap(snapshot.getBitmap());
}
private LatLngBounds getRandomBounds() {

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/snapshot_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription=""/>
</RelativeLayout>

View File

@@ -61,6 +61,7 @@
<string name="description_bottom_sheet">Show 2 MapView on screen with a bottom sheet</string>
<string name="description_map_snapshotter">Show a static bitmap taken with the MapSnapshotter</string>
<string name="description_map_snapshotter_reuse">Show how to reuse a MapSnapshotter instance</string>
<string name="description_map_snapshotter_marker">Show how to add a marker to a Snapshot</string>
<string name="description_camera_animator">Use Android SDK Animators to animate camera position changes</string>
<string name="description_symbol_generator">Use Android SDK Views as symbols</string>
</resources>

View File

@@ -61,6 +61,7 @@
<string name="activity_bottom_sheet">Bottom sheet</string>
<string name="activity_map_snapshotter">Map Snapshotter</string>
<string name="activity_map_snapshotter_reuse">Map Snapshotter Reuse</string>
<string name="activity_map_snapshotter_marker">Map Snapshot with marker</string>
<string name="activity_camera_animator">Animator animation</string>
<string name="activity_symbol_generator">SymbolGenerator</string>
</resources>

View File

@@ -59,7 +59,7 @@ macro(mbgl_platform_core)
PRIVATE platform/android/src/android_renderer_frontend.cpp
PRIVATE platform/android/src/android_renderer_frontend.hpp
# Snapshots
# Snapshots (core)
PRIVATE platform/default/mbgl/gl/headless_backend.cpp
PRIVATE platform/default/mbgl/gl/headless_backend.hpp
PRIVATE platform/default/mbgl/gl/headless_frontend.cpp
@@ -68,8 +68,6 @@ macro(mbgl_platform_core)
PRIVATE platform/default/mbgl/map/map_snapshotter.hpp
PRIVATE platform/linux/src/headless_backend_egl.cpp
PRIVATE platform/linux/src/headless_display_egl.cpp
PRIVATE platform/android/src/snapshotter/map_snapshotter.cpp
PRIVATE platform/android/src/snapshotter/map_snapshotter.hpp
)
target_include_directories(mbgl-core
@@ -293,6 +291,12 @@ add_library(mbgl-android STATIC
platform/android/src/offline/offline_region_status.cpp
platform/android/src/offline/offline_region_status.hpp
# Snapshots (SDK)
platform/android/src/snapshotter/map_snapshotter.cpp
platform/android/src/snapshotter/map_snapshotter.hpp
platform/android/src/snapshotter/map_snapshot.cpp
platform/android/src/snapshotter/map_snapshot.hpp
# Main jni bindings
platform/android/src/attach_env.cpp
platform/android/src/attach_env.hpp

View File

@@ -50,6 +50,7 @@
#include "style/sources/sources.hpp"
#include "style/light.hpp"
#include "snapshotter/map_snapshotter.hpp"
#include "snapshotter/map_snapshot.hpp"
namespace mbgl {
namespace android {
@@ -185,6 +186,7 @@ void registerNatives(JavaVM *vm) {
// Snapshotter
MapSnapshotter::registerNative(env);
MapSnapshot::registerNative(env);
}
} // namespace android

View File

@@ -0,0 +1,57 @@
#include "map_snapshot.hpp"
#include "../bitmap.hpp"
#include <memory>
namespace mbgl {
namespace android {
MapSnapshot::MapSnapshot(float pixelRatio_, MapSnapshot::PointForFn pointForFn_)
: pixelRatio(pixelRatio_)
, pointForFn(std::move(pointForFn_)) {
}
MapSnapshot::~MapSnapshot() = default;
jni::Object<PointF> MapSnapshot::pixelForLatLng(jni::JNIEnv& env, jni::Object<LatLng> jLatLng) {
ScreenCoordinate point = pointForFn(LatLng::getLatLng(env, jLatLng));
return PointF::New(env, point.x * pixelRatio, point.y * pixelRatio);
}
// Static methods //
jni::Object<MapSnapshot> MapSnapshot::New(JNIEnv& env,
PremultipliedImage&& image,
float pixelRatio,
mbgl::MapSnapshotter::PointForFn pointForFn) {
// Create the bitmap
auto bitmap = Bitmap::CreateBitmap(env, std::move(image));
// Create the Mapsnapshot peers
static auto constructor = javaClass.GetConstructor<jni::jlong, jni::Object<Bitmap>>(env);
auto nativePeer = std::make_unique<MapSnapshot>(pixelRatio, pointForFn);
return javaClass.New(env, constructor, reinterpret_cast<jlong>(nativePeer.release()), bitmap);
}
jni::Class<MapSnapshot> MapSnapshot::javaClass;
void MapSnapshot::registerNative(jni::JNIEnv& env) {
// Lookup the class
MapSnapshot::javaClass = *jni::Class<MapSnapshot>::Find(env).NewGlobalRef(env).release();
#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
// Register the peer
jni::RegisterNativePeer<MapSnapshot>(env, MapSnapshot::javaClass,
"nativePtr",
std::make_unique<MapSnapshot, JNIEnv&>,
"initialize",
"finalize",
METHOD(&MapSnapshot::pixelForLatLng, "pixelForLatLng")
);
}
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,41 @@
#pragma once
#include <mbgl/map/map_snapshotter.hpp>
#include <jni/jni.hpp>
#include "../geometry/lat_lng.hpp"
#include "../graphics/pointf.hpp"
namespace mbgl {
namespace android {
class MapSnapshot {
public:
using PointForFn = mbgl::MapSnapshotter::PointForFn;
static constexpr auto Name() { return "com/mapbox/mapboxsdk/snapshotter/MapSnapshot"; };
static void registerNative(jni::JNIEnv&);
static jni::Object<MapSnapshot> New(JNIEnv& env,
PremultipliedImage&& image,
float pixelRatio,
PointForFn pointForFn);
MapSnapshot(jni::JNIEnv&) {};
MapSnapshot(float pixelRatio, PointForFn);
~MapSnapshot();
jni::Object<PointF> pixelForLatLng(jni::JNIEnv&, jni::Object<LatLng>);
private:
static jni::Class<MapSnapshot> javaClass;
float pixelRatio;
mbgl::MapSnapshotter::PointForFn pointForFn;
};
} // namespace android
} // namespace mbgl

View File

@@ -8,7 +8,7 @@
#include <mbgl/actor/scheduler.hpp>
#include "../attach_env.hpp"
#include "../bitmap.hpp"
#include "map_snapshot.hpp"
namespace mbgl {
namespace android {
@@ -58,7 +58,7 @@ MapSnapshotter::~MapSnapshotter() = default;
void MapSnapshotter::start(JNIEnv&) {
MBGL_VERIFY_THREAD(tid);
snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::Callback>>(*Scheduler::GetCurrent(), [this](std::exception_ptr err, PremultipliedImage image) {
snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::Callback>>(*Scheduler::GetCurrent(), [this](std::exception_ptr err, PremultipliedImage image, mbgl::MapSnapshotter::PointForFn pointForFn) {
MBGL_VERIFY_THREAD(tid);
android::UniqueEnv _env = android::AttachEnv();
@@ -67,12 +67,12 @@ void MapSnapshotter::start(JNIEnv&) {
static auto onSnapshotFailed = javaClass.GetMethod<void (jni::String)>(*_env, "onSnapshotFailed");
javaPeer->Call(*_env, onSnapshotFailed, jni::Make<jni::String>(*_env, util::toString(err)));
} else {
// Create the bitmap
auto bitmap = Bitmap::CreateBitmap(*_env, std::move(image));
// Create the wrapper
auto mapSnapshot = android::MapSnapshot::New(*_env, std::move(image), pixelRatio, pointForFn);
// invoke callback
static auto onSnapshotReady = javaClass.GetMethod<void (jni::Object<Bitmap>)>(*_env, "onSnapshotReady");
javaPeer->Call(*_env, onSnapshotReady, bitmap);
static auto onSnapshotReady = javaClass.GetMethod<void (jni::Object<MapSnapshot>)>(*_env, "onSnapshotReady");
javaPeer->Call(*_env, onSnapshotReady, mapSnapshot);
}
});