[android] addImage / removeImage jni bindings
This commit is contained in:
committed by
Ivo van Dongen
parent
b470a65582
commit
8fbc1bc750
@@ -187,6 +187,27 @@ public class MapboxMap {
|
||||
getMapView().getNativeMapView().removeSource(sourceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an image to be used int hte map's style
|
||||
*
|
||||
* @param name the name of the image
|
||||
* @param image the pre-multiplied Bitmap
|
||||
*/
|
||||
@UiThread
|
||||
public void addImage(@NonNull String name, @NonNull Bitmap image) {
|
||||
getMapView().getNativeMapView().addImage(name, image);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an image from the map's style
|
||||
*
|
||||
* @param name the name of the image to remove
|
||||
*/
|
||||
@UiThread
|
||||
public void removeImage(String name) {
|
||||
getMapView().getNativeMapView().removeImage(name);
|
||||
}
|
||||
|
||||
//
|
||||
// MinZoom
|
||||
//
|
||||
|
||||
@@ -2,11 +2,13 @@ package com.mapbox.mapboxsdk.maps;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.PointF;
|
||||
import android.graphics.RectF;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.Surface;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
@@ -22,6 +24,7 @@ import com.mapbox.mapboxsdk.style.sources.NoSuchSourceException;
|
||||
import com.mapbox.mapboxsdk.style.sources.Source;
|
||||
import com.mapbox.services.commons.geojson.Feature;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -506,6 +509,27 @@ final class NativeMapView {
|
||||
nativeRemoveSource(nativeMapViewPtr, sourceId);
|
||||
}
|
||||
|
||||
public void addImage(@NonNull String name, @NonNull Bitmap image) {
|
||||
//Check/correct config
|
||||
if (image.getConfig() != Bitmap.Config.ARGB_8888) {
|
||||
image = image.copy(Bitmap.Config.ARGB_8888, false);
|
||||
}
|
||||
|
||||
//Get pixels
|
||||
ByteBuffer buffer = ByteBuffer.allocate(image.getByteCount());
|
||||
image.copyPixelsToBuffer(buffer);
|
||||
|
||||
//Determine pixel ratio
|
||||
float density = image.getDensity() == Bitmap.DENSITY_NONE ? Bitmap.DENSITY_NONE : image.getDensity();
|
||||
float pixelRatio = density / DisplayMetrics.DENSITY_DEFAULT;
|
||||
|
||||
nativeAddImage(nativeMapViewPtr, name, image.getWidth(), image.getHeight(), pixelRatio, buffer.array());
|
||||
}
|
||||
|
||||
public void removeImage(String name) {
|
||||
nativeRemoveImage(nativeMapViewPtr, name);
|
||||
}
|
||||
|
||||
// Feature querying
|
||||
|
||||
@NonNull
|
||||
@@ -722,6 +746,10 @@ final class NativeMapView {
|
||||
|
||||
private native void nativeRemoveSource(long nativeMapViewPtr, String sourceId) throws NoSuchSourceException;
|
||||
|
||||
private native void nativeAddImage(long nativeMapViewPtr, String name, int width, int height, float pixelRatio, byte[] array);
|
||||
|
||||
private native void nativeRemoveImage(long nativeMapViewPtr, String name);
|
||||
|
||||
private native void nativeUpdatePolygon(long nativeMapViewPtr, long polygonId, Polygon polygon);
|
||||
|
||||
private native void nativeUpdatePolyline(long nativeMapviewPtr, long polylineId, Polyline polyline);
|
||||
|
||||
@@ -1215,6 +1215,33 @@ void nativeRemoveSource(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr,
|
||||
}
|
||||
}
|
||||
|
||||
void nativeAddImage(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* name, jni::jint width, jni::jint height, jni::jfloat pixelRatio, jni::jarray<jbyte>* data) {
|
||||
assert(nativeMapViewPtr != 0);
|
||||
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
|
||||
|
||||
// Create Pre-multiplied image from byte[]
|
||||
NullCheck(*env, data);
|
||||
std::size_t size = jni::GetArrayLength(*env, *data);
|
||||
mbgl::PremultipliedImage premultipliedImage(width, height);
|
||||
|
||||
if (premultipliedImage.size() != uint32_t(size)) {
|
||||
throw mbgl::util::SpriteImageException("Sprite image pixel count mismatch");
|
||||
}
|
||||
|
||||
jni::GetArrayRegion(*env, *data, 0, size, reinterpret_cast<jbyte*>(premultipliedImage.data.get()));
|
||||
|
||||
//Wrap in a SpriteImage with the correct pixel ratio
|
||||
auto spriteImage = std::make_unique<mbgl::SpriteImage>(std::move(premultipliedImage), float(pixelRatio));
|
||||
|
||||
nativeMapView->getMap().addImage(std_string_from_jstring(env, name), std::move(spriteImage));
|
||||
}
|
||||
|
||||
void nativeRemoveImage(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* name) {
|
||||
assert(nativeMapViewPtr != 0);
|
||||
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
|
||||
nativeMapView->getMap().removeImage(std_string_from_jstring(env, name));
|
||||
}
|
||||
|
||||
void nativeScheduleTakeSnapshot(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr) {
|
||||
mbgl::Log::Debug(mbgl::Event::JNI, "nativeRenderToOffscreen");
|
||||
assert(nativeMapViewPtr != 0);
|
||||
@@ -1890,6 +1917,8 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
|
||||
MAKE_NATIVE_METHOD(nativeGetSource, "(JLjava/lang/String;)Lcom/mapbox/mapboxsdk/style/sources/Source;"),
|
||||
MAKE_NATIVE_METHOD(nativeAddSource, "(JJ)V"),
|
||||
MAKE_NATIVE_METHOD(nativeRemoveSource, "(JLjava/lang/String;)V"),
|
||||
MAKE_NATIVE_METHOD(nativeAddImage, "(JLjava/lang/String;IIF[B)V"),
|
||||
MAKE_NATIVE_METHOD(nativeRemoveImage, "(JLjava/lang/String;)V"),
|
||||
MAKE_NATIVE_METHOD(nativeSetContentPadding, "(JDDDD)V"),
|
||||
MAKE_NATIVE_METHOD(nativeScheduleTakeSnapshot, "(J)V"),
|
||||
MAKE_NATIVE_METHOD(nativeQueryRenderedFeaturesForPoint, "(JFF[Ljava/lang/String;)[Lcom/mapbox/services/commons/geojson/Feature;"),
|
||||
|
||||
Reference in New Issue
Block a user