Compare commits

...

13 Commits

Author SHA1 Message Date
Björn Ritzl
5db8d5c072 Update index.md 2025-06-04 07:47:27 +02:00
AGulev
851f3dee87 fix font 2024-02-27 07:55:43 +01:00
AGulev
73c3596156 fix manifest 2023-09-30 16:05:53 +02:00
Alexey Gulev
d58f5ec46b
Update build.gradle 2023-09-30 15:48:47 +02:00
Björn Ritzl
89de4ccd59
Merge pull request #11 from rthery/patch-1
Fix source code link for extension-camera
2023-02-08 22:34:27 +01:00
Romain Théry
e32dd144b6
Fix source code link for extension-camera 2023-02-08 21:16:38 +01:00
JCash
6d420fc011 Use constructor for dmScript::LuaHBuffer 2021-12-15 11:48:15 +01:00
Alexey Gulev
e07bd373ec Check supported parameters 2021-11-20 09:47:01 +01:00
Alexey Gulev
6b01fcb6d6 fix typo 2021-11-20 08:34:30 +01:00
Alexey Gulev
cb4ab80e04 possible fix for crash https://github.com/defold/extension-camera/issues/10 2021-11-20 08:31:45 +01:00
Björn Ritzl
406edd5c37 Update index.md 2021-11-18 10:09:27 +01:00
Alexey Gulev
8832fc1588
Merge pull request #9 from defold/mutex_scope
Smaller mutex scope + use new SDK android methods (Defold 1.2.188)
2021-11-10 15:26:59 +01:00
Alexey Gulev
4e0625d64e Use smaller scope for the callback's mutex to avoid possible ANRs/Crashes
Use new Defold SDK API (1.2.188) for attaching VM thread and loading java class
2021-11-10 09:27:37 +01:00
9 changed files with 97 additions and 145 deletions

View File

@ -1,18 +1,10 @@
name: "Camera" name: "Camera"
platforms: platforms:
x86_64-osx: osx:
context: context:
frameworks: ["AVFoundation", "CoreMedia"] frameworks: ["AVFoundation", "CoreMedia"]
x86-osx: ios:
context:
frameworks: ["AVFoundation", "CoreMedia"]
arm64-ios:
context:
frameworks: ["AVFoundation", "CoreMedia", "UIKit"]
armv7-ios:
context: context:
frameworks: ["AVFoundation", "CoreMedia", "UIKit"] frameworks: ["AVFoundation", "CoreMedia", "UIKit"]

View File

@ -1,3 +1,7 @@
dependencies { repositories {
compile 'androidx.core:core:1.2.0' mavenCentral()
}
dependencies {
implementation 'androidx.core:core:1.2.0'
} }

View File

@ -45,7 +45,7 @@ public class AndroidCamera extends Fragment
private boolean newFrame; private boolean newFrame;
private int position; private int position;
private int quality; private int quality;
private Camera.Size size; private Camera.Size previewSize;
private static Context context; private static Context context;
@ -132,26 +132,43 @@ public class AndroidCamera extends Fragment
Camera.Parameters params = camera.getParameters(); Camera.Parameters params = camera.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes(); List<Camera.Size> previewSizes = params.getSupportedPreviewSizes();
List<String> focusModes = params.getSupportedFocusModes();
List<Camera.Size> pictureSizes = params.getSupportedPictureSizes();
List<Integer> supportedFormats = params.getSupportedPictureFormats();
Camera.Size pictureSize;
switch(this.quality) switch(this.quality)
{ {
case CAPTURE_QUALITY_HIGH: case CAPTURE_QUALITY_HIGH:
this.size = sizes.get(sizes.size() - 1); this.previewSize = previewSizes.get(previewSizes.size() - 1);
pictureSize = pictureSizes.get(previewSizes.size() - 1);
break; break;
case CAPTURE_QUALITY_LOW: case CAPTURE_QUALITY_LOW:
this.size = sizes.get(0); this.previewSize = previewSizes.get(0);
pictureSize = pictureSizes.get(0);
break; break;
case CAPTURE_QUALITY_MEDIUM: case CAPTURE_QUALITY_MEDIUM:
default: default:
this.size = sizes.get((int)Math.ceil(sizes.size() / 2)); this.previewSize = previewSizes.get((int)Math.ceil(previewSizes.size() / 2));
pictureSize = pictureSizes.get((int)Math.ceil(previewSizes.size() / 2));
break; break;
} }
params.setPreviewSize(this.size.width, this.size.height); if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
params.setPictureSize(this.size.width, this.size.height); {
params.setPictureFormat(PixelFormat.JPEG); params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
params.setJpegQuality(90); }
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
params.setPreviewSize(this.previewSize.width, this.previewSize.height);
params.setPictureSize(pictureSize.width, pictureSize.height);
if (supportedFormats.contains(PixelFormat.JPEG))
{
params.setPictureFormat(PixelFormat.JPEG);
params.setJpegQuality(90);
}
camera.setParameters(params); camera.setParameters(params);
final Activity activity = (Activity)context; final Activity activity = (Activity)context;
@ -166,7 +183,7 @@ public class AndroidCamera extends Fragment
if(rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) if(rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)
flip = true; flip = true;
int[] pixels = convertYUV420_NV21toARGB8888(data, AndroidCamera.this.size.width, AndroidCamera.this.size.height, flip); int[] pixels = convertYUV420_NV21toARGB8888(data, AndroidCamera.this.previewSize.width, AndroidCamera.this.previewSize.height, flip);
frameUpdate(pixels); frameUpdate(pixels);
} }
}); });
@ -179,7 +196,7 @@ public class AndroidCamera extends Fragment
{ {
} }
captureStarted(this.size.width, this.size.height); captureStarted(this.previewSize.width, this.previewSize.height);
camera.startPreview(); camera.startPreview();
queueMessage(CAMERA_STARTED); queueMessage(CAMERA_STARTED);
} }

View File

@ -7,7 +7,6 @@
#define MODULE_NAME "camera" #define MODULE_NAME "camera"
// Defold SDK // Defold SDK
#define DLIB_LOG_DOMAIN LIB_NAME
#include <dmsdk/sdk.h> #include <dmsdk/sdk.h>
#if defined(DM_PLATFORM_IOS) || defined(DM_PLATFORM_OSX) || defined(DM_PLATFORM_ANDROID) #if defined(DM_PLATFORM_IOS) || defined(DM_PLATFORM_OSX) || defined(DM_PLATFORM_ANDROID)
@ -48,21 +47,30 @@ void Camera_QueueMessage(CameraMessage message)
static void Camera_ProcessQueue() static void Camera_ProcessQueue()
{ {
DM_MUTEX_SCOPED_LOCK(g_DefoldCamera.m_Mutex); if (g_DefoldCamera.m_MessageQueue.Empty())
{
return;
}
for (uint32_t i = 0; i != g_DefoldCamera.m_MessageQueue.Size(); ++i) dmArray<CameraMessage> tmp;
{
DM_MUTEX_SCOPED_LOCK(g_DefoldCamera.m_Mutex);
tmp.Swap(g_DefoldCamera.m_MessageQueue);
}
for (uint32_t i = 0; i != tmp.Size(); ++i)
{ {
lua_State* L = dmScript::GetCallbackLuaContext(g_DefoldCamera.m_Callback); lua_State* L = dmScript::GetCallbackLuaContext(g_DefoldCamera.m_Callback);
if (!dmScript::SetupCallback(g_DefoldCamera.m_Callback)) if (!dmScript::SetupCallback(g_DefoldCamera.m_Callback))
{ {
break; break;
} }
CameraMessage message = g_DefoldCamera.m_MessageQueue[i]; CameraMessage message = tmp[i];
if (message == CAMERA_STARTED) if (message == CAMERA_STARTED)
{ {
// Increase ref count // Increase ref count
dmScript::LuaHBuffer luabuffer = {g_DefoldCamera.m_VideoBuffer, false}; dmScript::LuaHBuffer luabuffer(g_DefoldCamera.m_VideoBuffer, dmScript::OWNER_C);
dmScript::PushBuffer(L, luabuffer); dmScript::PushBuffer(L, luabuffer);
g_DefoldCamera.m_VideoBufferLuaRef = dmScript::Ref(L, LUA_REGISTRYINDEX); g_DefoldCamera.m_VideoBufferLuaRef = dmScript::Ref(L, LUA_REGISTRYINDEX);
} }
@ -80,7 +88,6 @@ static void Camera_ProcessQueue()
} }
dmScript::TeardownCallback(g_DefoldCamera.m_Callback); dmScript::TeardownCallback(g_DefoldCamera.m_Callback);
} }
g_DefoldCamera.m_MessageQueue.SetSize(0);
} }
static void Camera_DestroyCallback() static void Camera_DestroyCallback()

View File

@ -3,6 +3,7 @@
#if defined(DM_PLATFORM_ANDROID) #if defined(DM_PLATFORM_ANDROID)
#include "camera_private.h" #include "camera_private.h"
#include <dmsdk/dlib/android.h>
static jclass g_CameraClass = 0; static jclass g_CameraClass = 0;
static jobject g_CameraObject = 0; static jobject g_CameraObject = 0;
@ -20,53 +21,20 @@ static CaptureQuality g_Quality;
static dmBuffer::HBuffer* g_Buffer = 0; static dmBuffer::HBuffer* g_Buffer = 0;
static JNIEnv* Attach()
{
JNIEnv* env;
JavaVM* vm = dmGraphics::GetNativeAndroidJavaVM();
vm->AttachCurrentThread(&env, NULL);
return env;
}
static bool Detach(JNIEnv* env)
{
bool exception = (bool) env->ExceptionCheck();
env->ExceptionClear();
JavaVM* vm = dmGraphics::GetNativeAndroidJavaVM();
vm->DetachCurrentThread();
return !exception;
}
static jclass GetClass(JNIEnv* env, const char* classname)
{
jclass activity_class = env->FindClass("android/app/NativeActivity");
jmethodID get_class_loader = env->GetMethodID(activity_class,"getClassLoader", "()Ljava/lang/ClassLoader;");
jobject cls = env->CallObjectMethod(dmGraphics::GetNativeAndroidActivity(), get_class_loader);
jclass class_loader = env->FindClass("java/lang/ClassLoader");
jmethodID find_class = env->GetMethodID(class_loader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
jstring str_class_name = env->NewStringUTF(classname);
jclass outcls = (jclass)env->CallObjectMethod(cls, find_class, str_class_name);
env->DeleteLocalRef(str_class_name);
return outcls;
}
extern "C" extern "C"
{ {
JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_frameUpdate(JNIEnv * env, jobject jobj, jintArray data); JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_frameUpdate(JNIEnv * env, jobject jobj, jintArray data);
JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_queueMessage(JNIEnv * env, jobject jobj, jint message); JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_queueMessage(JNIEnv * env, jobject jobj, jint message);
JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_captureStarted(JNIEnv * env, jobject jobj, jint width, jint height); JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_captureStarted(JNIEnv * env, jobject jobj, jint width, jint height);
} }
JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_frameUpdate(JNIEnv * env, jobject jobj, jintArray data) JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_frameUpdate(JNIEnv * env, jobject jobj, jintArray data)
{ {
if(!g_FrameLock) if(!g_FrameLock)
{ {
env->GetIntArrayRegion(data, 0, g_Width * g_Height, g_Data); env->GetIntArrayRegion(data, 0, g_Width * g_Height, g_Data);
g_FrameLock = true; g_FrameLock = true;
} }
} }
JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_queueMessage(JNIEnv * env, jobject jobj, jint message) JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_queueMessage(JNIEnv * env, jobject jobj, jint message)
@ -98,19 +66,15 @@ void CameraPlatform_GetCameraInfo(CameraInfo& outparams)
int CameraPlatform_Initialize() int CameraPlatform_Initialize()
{ {
JNIEnv* env = Attach(); dmAndroid::ThreadAttacher threadAttacher;
if(!env) JNIEnv* env = threadAttacher.GetEnv();
{
return false;
}
// get the AndroidCamera class // get the AndroidCamera class
jclass tmp = GetClass(env, "com.defold.android.camera/AndroidCamera"); jclass tmp = dmAndroid::LoadClass(env, "com.defold.android.camera/AndroidCamera");
g_CameraClass = (jclass)env->NewGlobalRef(tmp); g_CameraClass = (jclass)env->NewGlobalRef(tmp);
if(!g_CameraClass) if(!g_CameraClass)
{ {
dmLogError("Could not find class 'com.defold.android.camera/AndroidCamera'."); dmLogError("Could not find class 'com.defold.android.camera/AndroidCamera'.");
Detach(env);
return false; return false;
} }
@ -119,7 +83,6 @@ int CameraPlatform_Initialize()
if(!g_GetCameraMethodId) if(!g_GetCameraMethodId)
{ {
dmLogError("Could not get static method 'getCamera'."); dmLogError("Could not get static method 'getCamera'.");
Detach(env);
return false; return false;
} }
@ -128,7 +91,6 @@ int CameraPlatform_Initialize()
if(!g_CameraObject) if(!g_CameraObject)
{ {
dmLogError("Could not create instance."); dmLogError("Could not create instance.");
Detach(env);
return false; return false;
} }
@ -137,18 +99,15 @@ int CameraPlatform_Initialize()
if(!g_StartPreviewMethodId) if(!g_StartPreviewMethodId)
{ {
dmLogError("Could not get startPreview() method."); dmLogError("Could not get startPreview() method.");
Detach(env);
return false; return false;
} }
g_StopPreviewMethodId = env->GetMethodID(g_CameraClass, "stopPreview", "()V"); g_StopPreviewMethodId = env->GetMethodID(g_CameraClass, "stopPreview", "()V");
if(!g_StopPreviewMethodId) if(!g_StopPreviewMethodId)
{ {
dmLogError("Could not get stopPreview() method."); dmLogError("Could not get stopPreview() method.");
Detach(env);
return false; return false;
} }
Detach(env);
return true; return true;
} }
@ -164,9 +123,9 @@ void CameraPlatform_StartCapture(dmBuffer::HBuffer* buffer, CameraType type, Cap
g_Type = type; g_Type = type;
g_Quality = quality; g_Quality = quality;
JNIEnv* env = Attach(); dmAndroid::ThreadAttacher threadAttacher;
env->CallVoidMethod(g_CameraObject, g_StartPreviewMethodId); JNIEnv* env = threadAttacher.GetEnv();
Detach(env); env->CallVoidMethod(g_CameraObject, g_StartPreviewMethodId);
} }
void CameraPlatform_StopCapture() void CameraPlatform_StopCapture()
@ -177,15 +136,15 @@ void CameraPlatform_StopCapture()
return; return;
} }
JNIEnv* env = Attach(); dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
env->CallVoidMethod(g_CameraObject, g_StopPreviewMethodId); env->CallVoidMethod(g_CameraObject, g_StopPreviewMethodId);
Detach(env);
} }
void CameraPlatform_UpdateCapture() void CameraPlatform_UpdateCapture()
{ {
if(g_FrameLock) if(g_FrameLock)
{ {
int width = g_Width; int width = g_Width;
int height = g_Height; int height = g_Height;
int numChannels = 4; int numChannels = 4;
@ -209,8 +168,8 @@ void CameraPlatform_UpdateCapture()
out[index+2] = (argb>>16)&0xFF; // B out[index+2] = (argb>>16)&0xFF; // B
} }
} }
g_FrameLock = false; g_FrameLock = false;
} }
} }
#endif // DM_PLATFORM_ANDROID #endif // DM_PLATFORM_ANDROID

View File

@ -16,37 +16,10 @@ https://github.com/defold/extension-camera/archive/master.zip
We recommend using a link to a zip file of a [specific release](https://github.com/defold/extension-camera/releases). We recommend using a link to a zip file of a [specific release](https://github.com/defold/extension-camera/releases).
## Configuration
The extension can be configured by adding the following fields to game.project:
```
[adinfo]
tracking_usage_description = We would like to show you relevant ads.
register_for_attribution = 1
```
### tracking_usage_description
Before requesting advertising info and status on iOS 14 the application must request user authorization to access app-related data for tracking the user or the device. This is done automatically when `adinfo.get()` is called. The string set in `adinfo.tracking_usage_description` will be shown to the user.
Apple documentation: https://developer.apple.com/documentation/apptrackingtransparency?language=objc
### register_for_attribution
The extension can automatically register the application for ad network attribution using `SkAdNetwork` and the `registerAppForAdNetworkAttribution()` function. Enable this functionality by setting `adinfo.register_for_attribution` to 1 in game.project.
Apple documentation: https://developer.apple.com/documentation/storekit/skadnetwork
## Example ## Example
```lua Refer to the [example project](https://github.com/defold/extension-camera/blob/master/main/main.script).
function init(self)
adinfo.get(function(self, info)
print(info.ad_ident, info.ad_tracking_enabled)
end)
end
```
## FAQ ## FAQ
@ -61,7 +34,4 @@ tccutil reset Camera
## Source code ## Source code
The source code is available on [GitHub](https://github.com/defold/extension-adinfo) The source code is available on [GitHub](https://github.com/defold/extension-camera)
## API reference

8
main/default.font Normal file
View File

@ -0,0 +1,8 @@
font: "/builtins/fonts/vera_mo_bd.ttf"
material: "/builtins/fonts/font-df.material"
size: 14
antialias: 1
alpha: 1.0
shadow_alpha: 0.0
shadow_blur: 0
output_format: TYPE_DISTANCE_FIELD

View File

@ -16,14 +16,19 @@ embedded_instances {
" z: 0.0\n" " z: 0.0\n"
" w: 1.0\n" " w: 1.0\n"
" }\n" " }\n"
" property_decls {\n"
" }\n"
"}\n" "}\n"
"embedded_components {\n" "embedded_components {\n"
" id: \"sprite\"\n" " id: \"sprite\"\n"
" type: \"sprite\"\n" " type: \"sprite\"\n"
" data: \"tile_set: \\\"/main/camera.atlas\\\"\\n" " data: \"default_animation: \\\"logo\\\"\\n"
"default_animation: \\\"logo\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n" "material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n" "blend_mode: BLEND_MODE_ALPHA\\n"
"textures {\\n"
" sampler: \\\"texture_sampler\\\"\\n"
" texture: \\\"/main/camera.atlas\\\"\\n"
"}\\n"
"\"\n" "\"\n"
" position {\n" " position {\n"
" x: 0.0\n" " x: 0.0\n"
@ -60,10 +65,13 @@ embedded_instances {
data: "embedded_components {\n" data: "embedded_components {\n"
" id: \"sprite\"\n" " id: \"sprite\"\n"
" type: \"sprite\"\n" " type: \"sprite\"\n"
" data: \"tile_set: \\\"/main/logo.atlas\\\"\\n" " data: \"default_animation: \\\"logo\\\"\\n"
"default_animation: \\\"logo\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n" "material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n" "blend_mode: BLEND_MODE_ALPHA\\n"
"textures {\\n"
" sampler: \\\"texture_sampler\\\"\\n"
" texture: \\\"/main/logo.atlas\\\"\\n"
"}\\n"
"\"\n" "\"\n"
" position {\n" " position {\n"
" x: 0.0\n" " x: 0.0\n"
@ -86,12 +94,6 @@ embedded_instances {
" z: 0.0\\n" " z: 0.0\\n"
" w: 0.0\\n" " w: 0.0\\n"
"}\\n" "}\\n"
"scale {\\n"
" x: 1.0\\n"
" y: 1.0\\n"
" z: 1.0\\n"
" w: 0.0\\n"
"}\\n"
"color {\\n" "color {\\n"
" x: 1.0\\n" " x: 1.0\\n"
" y: 1.0\\n" " y: 1.0\\n"
@ -116,8 +118,8 @@ embedded_instances {
"blend_mode: BLEND_MODE_ALPHA\\n" "blend_mode: BLEND_MODE_ALPHA\\n"
"line_break: false\\n" "line_break: false\\n"
"text: \\\"Space/Touch to toggle capture\\\"\\n" "text: \\\"Space/Touch to toggle capture\\\"\\n"
"font: \\\"/builtins/fonts/system_font.font\\\"\\n" "font: \\\"/main/default.font\\\"\\n"
"material: \\\"/builtins/fonts/label.material\\\"\\n" "material: \\\"/builtins/fonts/label-df.material\\\"\\n"
"\"\n" "\"\n"
" position {\n" " position {\n"
" x: 90.0\n" " x: 90.0\n"
@ -140,12 +142,6 @@ embedded_instances {
" z: 0.0\\n" " z: 0.0\\n"
" w: 0.0\\n" " w: 0.0\\n"
"}\\n" "}\\n"
"scale {\\n"
" x: 1.0\\n"
" y: 1.0\\n"
" z: 1.0\\n"
" w: 0.0\\n"
"}\\n"
"color {\\n" "color {\\n"
" x: 1.0\\n" " x: 1.0\\n"
" y: 1.0\\n" " y: 1.0\\n"
@ -172,8 +168,8 @@ embedded_instances {
"text: \\\"label\\\\n" "text: \\\"label\\\\n"
"\\\"\\n" "\\\"\\n"
" \\\"\\\"\\n" " \\\"\\\"\\n"
"font: \\\"/builtins/fonts/system_font.font\\\"\\n" "font: \\\"/main/default.font\\\"\\n"
"material: \\\"/builtins/fonts/label.material\\\"\\n" "material: \\\"/builtins/fonts/label-df.material\\\"\\n"
"\"\n" "\"\n"
" position {\n" " position {\n"
" x: 90.0\n" " x: 90.0\n"

View File

@ -3,7 +3,6 @@ local function stop_capture(self)
if self.cameraframe == nil then if self.cameraframe == nil then
return return
end end
self.cameraframe = nil self.cameraframe = nil
camera.stop_capture() camera.stop_capture()