Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[android] [WIP] Create snapshot wrapper #10048

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
import com.mapbox.mapboxsdk.snapshotter.Snapshot;
import com.mapbox.mapboxsdk.style.layers.Filter;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.light.Light;
Expand Down Expand Up @@ -2461,7 +2462,7 @@ public interface SnapshotReadyCallback {
*
* @param snapshot the snapshot bitmap
*/
void onSnapshotReady(Bitmap snapshot);
void onSnapshotReady(Snapshot snapshot);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.geometry.ProjectedMeters;
import com.mapbox.mapboxsdk.maps.renderer.MapRenderer;
import com.mapbox.mapboxsdk.snapshotter.Snapshot;
import com.mapbox.mapboxsdk.storage.FileSource;
import com.mapbox.mapboxsdk.style.layers.CannotAddLayerException;
import com.mapbox.mapboxsdk.style.layers.Filter;
Expand Down Expand Up @@ -827,14 +828,18 @@ protected void onMapChanged(int rawChange) {
}
}

protected void onSnapshotReady(Bitmap mapContent) {
protected void onSnapshotReady(Snapshot mapContent) {
if (isDestroyedOn("OnSnapshotReady")) {
return;
}

Bitmap viewContent = BitmapUtils.createBitmapFromView(mapView);
if (snapshotReadyCallback != null && mapContent != null && viewContent != null) {
snapshotReadyCallback.onSnapshotReady(BitmapUtils.mergeBitmap(mapContent, viewContent));
Bitmap viewContentBitmap = BitmapUtils.createBitmapFromView(mapView);
if (snapshotReadyCallback != null && mapContent != null && mapContent.getBitmap() != null
&& viewContentBitmap != null) {
Bitmap mapContentBitmap = mapContent.getBitmap();
Bitmap mergedBitmap = BitmapUtils.mergeBitmap(mapContentBitmap, viewContentBitmap);
Snapshot mergedSnapshot = new Snapshot(mergedBitmap);
snapshotReadyCallback.onSnapshotReady(mergedSnapshot);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void start(@NonNull MapboxMap.SnapshotReadyCallback callback) {
* Starts loading and rendering the snapshot. The callbacks will be fired
* on the calling thread.
*
* @param callback the callback to use when the snapshot is ready
* @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) {
Expand Down Expand Up @@ -217,12 +217,14 @@ protected void addOverlay(Bitmap original) {
* 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(Snapshot snapshot) {
if (callback != null) {
Bitmap bitmap = snapshot.getBitmap();
addOverlay(bitmap);
callback.onSnapshotReady(bitmap);
Snapshot overlaidSnapshot = new Snapshot(bitmap);
callback.onSnapshotReady(overlaidSnapshot);
reset();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mapbox.mapboxsdk.snapshotter;

import android.graphics.Bitmap;


public class Snapshot {
private final Bitmap bitmap;

public Snapshot(Bitmap bitmap) {
this.bitmap = bitmap;
}

public Bitmap getBitmap() {
return bitmap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.snapshotter.Snapshot;
import com.mapbox.mapboxsdk.testapp.R;

/**
Expand Down Expand Up @@ -48,10 +49,11 @@ public void onClick(View view) {
}

@Override
public void onSnapshotReady(Bitmap snapshot) {
public void onSnapshotReady(Snapshot snapshot) {
PrintHelper photoPrinter = new PrintHelper(this);
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
photoPrinter.printBitmap("map.jpg - mapbox print job", snapshot);
Bitmap bitmap = snapshot.getBitmap();
photoPrinter.printBitmap("map.jpg - mapbox print job", bitmap);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.snapshotter.Snapshot;
import com.mapbox.mapboxsdk.testapp.R;

import java.util.Locale;
Expand Down Expand Up @@ -51,11 +52,12 @@ public void onClick(View view) {
final long startTime = System.nanoTime();
mapboxMap.snapshot(new MapboxMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap snapshot) {
public void onSnapshotReady(Snapshot snapshot) {
long endTime = System.nanoTime();
long duration = (long) ((endTime - startTime) / 1e6);
ImageView snapshotView = (ImageView) findViewById(R.id.imageView);
snapshotView.setImageBitmap(snapshot);
Bitmap bitmap = snapshot.getBitmap();
snapshotView.setImageBitmap(bitmap);
Toast.makeText(
SnapshotActivity.this,
String.format(Locale.getDefault(), "Snapshot taken in %d ms", duration),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter;
import com.mapbox.mapboxsdk.snapshotter.Snapshot;
import com.mapbox.mapboxsdk.testapp.R;

import java.util.ArrayList;
Expand Down Expand Up @@ -98,10 +99,11 @@ private void startSnapShot(final int row, final int column) {

snapshotter.start(new MapboxMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap snapshot) {
public void onSnapshotReady(Snapshot snapshot) {
Timber.i("Got the snapshot");
ImageView imageView = new ImageView(MapSnapshotterActivity.this);
imageView.setImageBitmap(snapshot);
Bitmap bitmap = snapshot.getBitmap();
imageView.setImageBitmap(bitmap);
grid.addView(
imageView,
new GridLayout.LayoutParams(GridLayout.spec(row), GridLayout.spec(column))
Expand Down
2 changes: 2 additions & 0 deletions platform/android/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ macro(mbgl_platform_core)
PRIVATE platform/linux/src/headless_display_egl.cpp
PRIVATE platform/android/src/snapshotter/map_snapshotter.cpp
PRIVATE platform/android/src/snapshotter/map_snapshotter.hpp
PRIVATE platform/android/src/snapshotter/snapshot.cpp
PRIVATE platform/android/src/snapshotter/snapshot.hpp
)

target_include_directories(mbgl-core
Expand Down
2 changes: 2 additions & 0 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "style/sources/sources.hpp"
#include "style/light.hpp"
#include "snapshotter/map_snapshotter.hpp"
#include "snapshotter/snapshot.hpp"

namespace mbgl {
namespace android {
Expand Down Expand Up @@ -185,6 +186,7 @@ void registerNatives(JavaVM *vm) {

// Snapshotter
MapSnapshotter::registerNative(env);
Snapshot::registerNative(env);
}

} // namespace android
Expand Down
6 changes: 4 additions & 2 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "conversion/collection.hpp"
#include "style/conversion/filter.hpp"
#include "geojson/conversion/feature.hpp"
#include "snapshotter/snapshot.hpp"

#include "jni.hpp"
#include "attach_env.hpp"
Expand Down Expand Up @@ -399,10 +400,11 @@ void NativeMapView::scheduleSnapshot(jni::JNIEnv&) {
auto _env = android::AttachEnv();
// Convert image to bitmap
auto bitmap = Bitmap::CreateBitmap(*_env, std::move(image));
auto snapshot = Snapshot::New(*_env, bitmap);

// invoke Mapview#OnSnapshotReady
static auto onSnapshotReady = javaClass.GetMethod<void (jni::Object<Bitmap>)>(*_env, "onSnapshotReady");
javaPeer->Call(*_env, onSnapshotReady, bitmap);
static auto onSnapshotReady = javaClass.GetMethod<void (jni::Object<Snapshot>)>(*_env, "onSnapshotReady");
javaPeer->Call(*_env, onSnapshotReady, snapshot);
});
}

Expand Down
6 changes: 4 additions & 2 deletions platform/android/src/snapshotter/map_snapshotter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "map_snapshotter.hpp"
#include "snapshot.hpp"

#include <mbgl/renderer/renderer.hpp>
#include <mbgl/style/style.hpp>
Expand Down Expand Up @@ -69,10 +70,11 @@ void MapSnapshotter::start(JNIEnv&) {
} else {
// Create the bitmap
auto bitmap = Bitmap::CreateBitmap(*_env, std::move(image));
auto snapshot = Snapshot::New(*_env, bitmap);

// 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<Snapshot>)>(*_env, "onSnapshotReady");
javaPeer->Call(*_env, onSnapshotReady, snapshot);
}
});

Expand Down
22 changes: 22 additions & 0 deletions platform/android/src/snapshotter/snapshot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "snapshot.hpp"

#include "../bitmap.hpp"

namespace mbgl {
namespace android {

jni::Object<Snapshot> Snapshot::New(jni::JNIEnv& env, jni::Object<Bitmap> image) {
static auto constructor = Snapshot::javaClass.GetConstructor<jni::Object<Bitmap>>(env);
return Snapshot::javaClass.New(env, constructor, image);
}

void Snapshot::registerNative(jni::JNIEnv& env) {
// Lookup the class
Snapshot::javaClass = *jni::Class<Snapshot>::Find(env).NewGlobalRef(env).release();
}

jni::Class<Snapshot> Snapshot::javaClass;


} // namespace android
} // namespace mbgl
25 changes: 25 additions & 0 deletions platform/android/src/snapshotter/snapshot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <mbgl/util/noncopyable.hpp>

#include <jni/jni.hpp>

namespace mbgl {
namespace android {

class Bitmap;

class Snapshot : private mbgl::util::noncopyable {
public:

static constexpr auto Name() { return "com/mapbox/mapboxsdk/snapshotter/Snapshot"; };

static jni::Object<Snapshot> New(jni::JNIEnv&, jni::Object<Bitmap> image);

static jni::Class<Snapshot> javaClass;

static void registerNative(jni::JNIEnv&);
};

} // namespace android
} // namespace mbgl