mirror of
https://github.com/defold/extension-camera
synced 2025-06-27 10:27:45 +02:00
Cleanup
This commit is contained in:
parent
c61161724a
commit
599f899f07
@ -22,42 +22,16 @@ import android.view.Surface;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class AndroidCamera
|
||||
public class AndroidCamera extends Fragment
|
||||
{
|
||||
private static final String TAG = AndroidCamera.class.getSimpleName();
|
||||
private static final String PERMISSION_FRAGMENT_TAG = PermissionsFragment.class.getSimpleName();
|
||||
|
||||
public static class PermissionsFragment extends Fragment {
|
||||
private AndroidCamera camera;
|
||||
|
||||
public PermissionsFragment(final Activity activity, final AndroidCamera camera) {
|
||||
this.camera = camera;
|
||||
final FragmentManager fragmentManager = activity.getFragmentManager();
|
||||
if (fragmentManager.findFragmentByTag(PERMISSION_FRAGMENT_TAG) == null) {
|
||||
fragmentManager.beginTransaction().add(this, PERMISSION_FRAGMENT_TAG).commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setRetainInstance(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
Log.d(TAG, "onRequestPermissionsResult " + requestCode + " " + permissions[0] + " " + grantResults[0]);
|
||||
camera.onRequestPermissionsResult(grantResults[0]);
|
||||
}
|
||||
}
|
||||
|
||||
enum CameraMessage
|
||||
{
|
||||
CAMERA_STARTED(0),
|
||||
CAMERA_STOPPED(1),
|
||||
CAMERA_NOT_PERMITTED(2),
|
||||
CAMERA_ERROR(3),
|
||||
CAMERA_SHOW_PERMISSION_RATIONALE(4);
|
||||
CAMERA_ERROR(3);
|
||||
|
||||
private final int value;
|
||||
|
||||
@ -70,7 +44,6 @@ class AndroidCamera
|
||||
}
|
||||
}
|
||||
|
||||
private PermissionsFragment permissionFragment;
|
||||
private Camera camera;
|
||||
private SurfaceTexture surface;
|
||||
private boolean newFrame;
|
||||
@ -89,42 +62,49 @@ class AndroidCamera
|
||||
private AndroidCamera(final Context context)
|
||||
{
|
||||
this.context = context;
|
||||
permissionFragment = new PermissionsFragment((Activity)context, this);
|
||||
}
|
||||
|
||||
private void requestPermission() {
|
||||
Log.d(TAG, "requestPermission");
|
||||
final Activity activity = (Activity)context;
|
||||
if (Build.VERSION.SDK_INT < 23)
|
||||
{
|
||||
Log.d(TAG, "requestPermission SDK_INT < 23");
|
||||
final int grantResult = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
|
||||
onRequestPermissionsResult(grantResult);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.d(TAG, "requestPermission fragment");
|
||||
final FragmentManager fragmentManager = activity.getFragmentManager();
|
||||
final Fragment fragment = fragmentManager.findFragmentByTag(PERMISSION_FRAGMENT_TAG);
|
||||
final String[] permissions = new String[] { Manifest.permission.CAMERA };
|
||||
fragment.requestPermissions(permissions, 100);
|
||||
final FragmentManager fragmentManager = ((Activity)context).getFragmentManager();
|
||||
if (fragmentManager.findFragmentByTag(TAG) == null) {
|
||||
fragmentManager.beginTransaction().add(this, TAG).commit();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void onRequestPermissionsResult(int grantResult) {
|
||||
Log.d(TAG, "onRequestPermissionsResult " + grantResult);
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setRetainInstance(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
int grantResult = grantResults[0];
|
||||
if (grantResult == PackageManager.PERMISSION_GRANTED)
|
||||
{
|
||||
Log.d(TAG, "onRequestPermissionsResult startPreviewAuthorized");
|
||||
startPreviewAuthorized();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.d(TAG, "onRequestPermissionsResult ERROR");
|
||||
queueMessage(CameraMessage.CAMERA_ERROR.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private void requestPermission() {
|
||||
final Activity activity = (Activity)context;
|
||||
if (Build.VERSION.SDK_INT < 23)
|
||||
{
|
||||
final int grantResult = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
|
||||
// onRequestPermissionsResult(grantResult);
|
||||
onRequestPermissionsResult(0, new String[] { Manifest.permission.CAMERA }, new int[] { grantResult });
|
||||
}
|
||||
else
|
||||
{
|
||||
final FragmentManager fragmentManager = activity.getFragmentManager();
|
||||
final Fragment fragment = fragmentManager.findFragmentByTag(TAG);
|
||||
final String[] permissions = new String[] { Manifest.permission.CAMERA };
|
||||
fragment.requestPermissions(permissions, 100);
|
||||
}
|
||||
}
|
||||
|
||||
private void startPreviewAuthorized()
|
||||
{
|
||||
CameraInfo info = new CameraInfo();
|
||||
@ -182,14 +162,13 @@ class AndroidCamera
|
||||
catch(IOException ioe)
|
||||
{
|
||||
}
|
||||
Log.d(TAG, "startPreviewAuthorized starting camera");
|
||||
|
||||
camera.startPreview();
|
||||
queueMessage(CameraMessage.CAMERA_STARTED.getValue());
|
||||
}
|
||||
|
||||
public void startPreview()
|
||||
{
|
||||
Log.d(TAG, "startPreview");
|
||||
if(camera != null)
|
||||
{
|
||||
queueMessage(CameraMessage.CAMERA_STARTED.getValue());
|
||||
|
@ -61,7 +61,6 @@ static void Camera_ProcessQueue()
|
||||
|
||||
if (message == CAMERA_STARTED)
|
||||
{
|
||||
dmLogInfo("Camera_ProcessQueue CAMERA_STARTED");
|
||||
// Increase ref count
|
||||
dmScript::LuaHBuffer luabuffer = {g_DefoldCamera.m_VideoBuffer, false};
|
||||
dmScript::PushBuffer(L, luabuffer);
|
||||
@ -69,14 +68,9 @@ static void Camera_ProcessQueue()
|
||||
}
|
||||
else if (message == CAMERA_STOPPED)
|
||||
{
|
||||
dmLogInfo("Camera_ProcessQueue CAMERA_STOPPED");
|
||||
dmScript::Unref(L, LUA_REGISTRYINDEX, g_DefoldCamera.m_VideoBufferLuaRef); // We want it destroyed by the GC
|
||||
g_DefoldCamera.m_VideoBufferLuaRef = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
dmLogInfo("Camera_ProcessQueue SOMETHING ELSE");
|
||||
}
|
||||
|
||||
lua_pushnumber(L, (lua_Number)message);
|
||||
int ret = lua_pcall(L, 2, 0, 0);
|
||||
|
@ -8,12 +8,10 @@ static const uint32_t CAMERA_WIDTH = 640;
|
||||
static const uint32_t CAMERA_HEIGHT = 480;
|
||||
|
||||
static jclass g_cameraClass = 0;
|
||||
static jmethodID g_initMethodId = 0;
|
||||
static jobject g_cameraObject = 0;
|
||||
static jmethodID g_getCameraMethodId = 0;
|
||||
static jmethodID g_startPreviewMethodId = 0;
|
||||
static jmethodID g_stopPreviewMethodId = 0;
|
||||
static jmethodID g_getCameraMethodId = 0;
|
||||
static jmethodID g_setCallbackDataMethodId = 0;
|
||||
static jobject g_cameraObject = 0;
|
||||
|
||||
static jint g_data[CAMERA_WIDTH * CAMERA_HEIGHT];
|
||||
static bool g_frameLock = false;
|
||||
@ -21,7 +19,6 @@ static bool g_frameLock = false;
|
||||
static dmBuffer::HBuffer g_VideoBuffer = 0;
|
||||
|
||||
|
||||
|
||||
static JNIEnv* Attach()
|
||||
{
|
||||
JNIEnv* env;
|
||||
@ -71,7 +68,6 @@ JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_frameUpdate(
|
||||
|
||||
JNIEXPORT void JNICALL Java_com_defold_android_camera_AndroidCamera_queueMessage(JNIEnv * env, jobject jobj, jint message)
|
||||
{
|
||||
dmLogInfo("Java_com_defold_android_camera_AndroidCamera_queueMessage %d", (int)message);
|
||||
Camera_QueueMessage((CameraMessage)message);
|
||||
}
|
||||
|
||||
@ -85,8 +81,6 @@ int CameraPlatform_Initialize()
|
||||
}
|
||||
|
||||
// get the AndroidCamera class
|
||||
if(!g_cameraClass)
|
||||
{
|
||||
jclass tmp = GetClass(env, "com.defold.android.camera/AndroidCamera");
|
||||
g_cameraClass = (jclass)env->NewGlobalRef(tmp);
|
||||
if(!g_cameraClass)
|
||||
@ -95,11 +89,8 @@ int CameraPlatform_Initialize()
|
||||
Detach(env);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// get an instance of the AndroidCamera class using the getCamera() method
|
||||
if(!g_getCameraMethodId)
|
||||
{
|
||||
g_getCameraMethodId = env->GetStaticMethodID(g_cameraClass, "getCamera", "(Landroid/content/Context;)Lcom/defold/android/camera/AndroidCamera;");
|
||||
if(!g_getCameraMethodId)
|
||||
{
|
||||
@ -107,23 +98,30 @@ int CameraPlatform_Initialize()
|
||||
Detach(env);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(!g_cameraObject)
|
||||
{
|
||||
|
||||
jobject tmp1 = env->CallStaticObjectMethod(g_cameraClass, g_getCameraMethodId, dmGraphics::GetNativeAndroidActivity());
|
||||
g_cameraObject = (jobject)env->NewGlobalRef(tmp1);
|
||||
if(!g_cameraObject)
|
||||
{
|
||||
dmLogError("Could not create instance.");
|
||||
Detach(env);
|
||||
return false;
|
||||
}
|
||||
|
||||
// get reference to startPreview() and stopPreview() methods
|
||||
g_startPreviewMethodId = env->GetMethodID(g_cameraClass, "startPreview", "()V");
|
||||
if(!g_startPreviewMethodId)
|
||||
{
|
||||
g_startPreviewMethodId = env->GetMethodID(g_cameraClass, "startPreview", "()V");
|
||||
assert(g_startPreviewMethodId);
|
||||
dmLogError("Could not get startPreview() method.");
|
||||
Detach(env);
|
||||
return false;
|
||||
}
|
||||
g_stopPreviewMethodId = env->GetMethodID(g_cameraClass, "stopPreview", "()V");
|
||||
if(!g_stopPreviewMethodId)
|
||||
{
|
||||
g_stopPreviewMethodId = env->GetMethodID(g_cameraClass, "stopPreview", "()V");
|
||||
assert(g_stopPreviewMethodId);
|
||||
dmLogError("Could not get stopPreview() method.");
|
||||
Detach(env);
|
||||
return false;
|
||||
}
|
||||
|
||||
Detach(env);
|
||||
@ -132,7 +130,12 @@ int CameraPlatform_Initialize()
|
||||
|
||||
void CameraPlatform_StartCapture(dmBuffer::HBuffer* buffer, CameraType type, CaptureQuality quality, CameraInfo& outparams)
|
||||
{
|
||||
dmLogInfo("CameraPlatform_StartCapture");
|
||||
if (!g_cameraObject)
|
||||
{
|
||||
Camera_QueueMessage(CAMERA_ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
outparams.m_Width = (uint32_t)CAMERA_WIDTH;
|
||||
outparams.m_Height = (uint32_t)CAMERA_HEIGHT;
|
||||
|
||||
@ -151,24 +154,23 @@ void CameraPlatform_StartCapture(dmBuffer::HBuffer* buffer, CameraType type, Cap
|
||||
|
||||
g_VideoBuffer = *buffer;
|
||||
|
||||
if (g_cameraObject)
|
||||
{
|
||||
dmLogInfo("CameraPlatform_StartCapture JNI");
|
||||
JNIEnv* env = Attach();
|
||||
env->CallVoidMethod(g_cameraObject, g_startPreviewMethodId);
|
||||
Detach(env);
|
||||
}
|
||||
}
|
||||
|
||||
void CameraPlatform_StopCapture()
|
||||
{
|
||||
if (g_cameraObject)
|
||||
if (!g_cameraObject)
|
||||
{
|
||||
Camera_QueueMessage(CAMERA_ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
JNIEnv* env = Attach();
|
||||
env->CallVoidMethod(g_cameraObject, g_stopPreviewMethodId);
|
||||
Detach(env);
|
||||
}
|
||||
}
|
||||
|
||||
void CameraPlatform_UpdateCapture()
|
||||
{
|
||||
|
@ -27,8 +27,7 @@ enum CameraMessage
|
||||
CAMERA_STARTED,
|
||||
CAMERA_STOPPED,
|
||||
CAMERA_NOT_PERMITTED,
|
||||
CAMERA_ERROR,
|
||||
CAMERA_SHOW_PERMISSION_RATIONALE
|
||||
CAMERA_ERROR
|
||||
};
|
||||
|
||||
extern int CameraPlatform_Initialize();
|
||||
|
@ -26,8 +26,6 @@ local function start_capture(self)
|
||||
end
|
||||
|
||||
camera.start_capture(type, quality, function(self, status)
|
||||
print("camera.start_capture", status)
|
||||
pprint(camera)
|
||||
if status == camera.CAMERA_STARTED then
|
||||
self.cameraframe = camera.get_frame()
|
||||
self.camerainfo = camera.get_info()
|
||||
@ -39,7 +37,9 @@ local function start_capture(self)
|
||||
num_mip_maps=1
|
||||
}
|
||||
label.set_text("logo#status", "Capture Status: ON")
|
||||
else
|
||||
elseif status == camera.CAMERA_STOPPED then
|
||||
label.set_text("logo#status", "Capture Status: OFF")
|
||||
elseif status == camera.CAMERA_ERROR then
|
||||
label.set_text("logo#status", "Capture Status: ERROR")
|
||||
end
|
||||
end)
|
||||
|
Loading…
x
Reference in New Issue
Block a user