forked from dotnet/android-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
417 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
384 changes: 28 additions & 356 deletions
384
android5.0/Camera2Basic/Camera2Basic/Camera2BasicFragment.cs
Large diffs are not rendered by default.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
android5.0/Camera2Basic/Camera2Basic/CompareSizesByArea.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Android.Util; | ||
using Java.Lang; | ||
using Java.Util; | ||
|
||
namespace Camera2Basic | ||
{ | ||
public class CompareSizesByArea : Java.Lang.Object, IComparator | ||
{ | ||
public int Compare(Object lhs, Object rhs) | ||
{ | ||
var lhsSize = (Size)lhs; | ||
var rhsSize = (Size)rhs; | ||
// We cast here to ensure the multiplications won't overflow | ||
return Long.Signum((long)lhsSize.Width * lhsSize.Height - (long)rhsSize.Width * rhsSize.Height); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
android5.0/Camera2Basic/Camera2Basic/ConfirmationDialog.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
using Android; | ||
using Android.App; | ||
using Android.Content; | ||
using Android.OS; | ||
using Android.Support.V13.App; | ||
|
||
namespace Camera2Basic | ||
{ | ||
public class ConfirmationDialog : DialogFragment | ||
{ | ||
private static Fragment mParent; | ||
private class PositiveListener : Java.Lang.Object, IDialogInterfaceOnClickListener | ||
{ | ||
public void OnClick(IDialogInterface dialog, int which) | ||
{ | ||
FragmentCompat.RequestPermissions(mParent, | ||
new string[] { Manifest.Permission.Camera }, Camera2BasicFragment.REQUEST_CAMERA_PERMISSION); | ||
} | ||
} | ||
|
||
private class NegativeListener : Java.Lang.Object, IDialogInterfaceOnClickListener | ||
{ | ||
public void OnClick(IDialogInterface dialog, int which) | ||
{ | ||
Activity activity = mParent.Activity; | ||
if (activity != null) | ||
{ | ||
activity.Finish(); | ||
} | ||
} | ||
} | ||
|
||
public override Dialog OnCreateDialog(Bundle savedInstanceState) | ||
{ | ||
mParent = ParentFragment; | ||
return new AlertDialog.Builder(Activity) | ||
.SetMessage(Resource.String.request_permission) | ||
.SetPositiveButton(Android.Resource.String.Ok, new PositiveListener()) | ||
.SetNegativeButton(Android.Resource.String.Cancel, new NegativeListener()) | ||
.Create(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
using Android.App; | ||
using Android.Content; | ||
using Android.OS; | ||
|
||
namespace Camera2Basic | ||
{ | ||
public class ErrorDialog : DialogFragment | ||
{ | ||
private static readonly string ARG_MESSAGE = "message"; | ||
private static Activity mActivity; | ||
|
||
private class PositiveListener : Java.Lang.Object, IDialogInterfaceOnClickListener | ||
{ | ||
public void OnClick(IDialogInterface dialog, int which) | ||
{ | ||
mActivity.Finish(); | ||
} | ||
} | ||
|
||
public static ErrorDialog NewInstance(string message) | ||
{ | ||
var args = new Bundle(); | ||
args.PutString(ARG_MESSAGE, message); | ||
return new ErrorDialog { Arguments = args }; | ||
} | ||
|
||
public override Dialog OnCreateDialog(Bundle savedInstanceState) | ||
{ | ||
mActivity = Activity; | ||
return new AlertDialog.Builder(mActivity) | ||
.SetMessage(Arguments.GetString(ARG_MESSAGE)) | ||
.SetPositiveButton(Android.Resource.String.Ok, new PositiveListener()) | ||
.Create(); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
android5.0/Camera2Basic/Camera2Basic/Listeners/Camera2BasicSurfaceTextureListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
using Android.Views; | ||
|
||
namespace Camera2Basic.Listeners | ||
{ | ||
public class Camera2BasicSurfaceTextureListener : Java.Lang.Object, TextureView.ISurfaceTextureListener | ||
{ | ||
public Camera2BasicFragment Owner { get; set; } | ||
|
||
public Camera2BasicSurfaceTextureListener(Camera2BasicFragment owner) | ||
{ | ||
Owner = owner; | ||
} | ||
|
||
public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int width, int height) | ||
{ | ||
Owner.OpenCamera(width, height); | ||
} | ||
|
||
public bool OnSurfaceTextureDestroyed(Android.Graphics.SurfaceTexture surface) | ||
{ | ||
return true; | ||
} | ||
|
||
public void OnSurfaceTextureSizeChanged(Android.Graphics.SurfaceTexture surface, int width, int height) | ||
{ | ||
Owner.ConfigureTransform(width, height); | ||
} | ||
|
||
public void OnSurfaceTextureUpdated(Android.Graphics.SurfaceTexture surface) | ||
{ | ||
|
||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
android5.0/Camera2Basic/Camera2Basic/Listeners/CameraCaptureListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
using Android.Hardware.Camera2; | ||
using Java.IO; | ||
using Java.Lang; | ||
|
||
namespace Camera2Basic.Listeners | ||
{ | ||
public class CameraCaptureListener : CameraCaptureSession.CaptureCallback | ||
{ | ||
public Camera2BasicFragment Owner { get; set; } | ||
public File File { get; set; } | ||
public override void OnCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) | ||
{ | ||
Process(result); | ||
} | ||
|
||
public override void OnCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult) | ||
{ | ||
Process(partialResult); | ||
} | ||
|
||
private void Process(CaptureResult result) | ||
{ | ||
switch (Owner.mState) | ||
{ | ||
case Camera2BasicFragment.STATE_WAITING_LOCK: | ||
{ | ||
Integer afState = (Integer)result.Get(CaptureResult.ControlAfState); | ||
if (afState == null) | ||
{ | ||
Owner.CaptureStillPicture(); | ||
} | ||
|
||
else if ((((int)ControlAFState.FocusedLocked) == afState.IntValue()) || | ||
(((int)ControlAFState.NotFocusedLocked) == afState.IntValue())) | ||
{ | ||
// ControlAeState can be null on some devices | ||
Integer aeState = (Integer)result.Get(CaptureResult.ControlAeState); | ||
if (aeState == null || | ||
aeState.IntValue() == ((int)ControlAEState.Converged)) | ||
{ | ||
Owner.mState = Camera2BasicFragment.STATE_PICTURE_TAKEN; | ||
Owner.CaptureStillPicture(); | ||
} | ||
else | ||
{ | ||
Owner.RunPrecaptureSequence(); | ||
} | ||
} | ||
break; | ||
} | ||
case Camera2BasicFragment.STATE_WAITING_PRECAPTURE: | ||
{ | ||
// ControlAeState can be null on some devices | ||
Integer aeState = (Integer)result.Get(CaptureResult.ControlAeState); | ||
if (aeState == null || | ||
aeState.IntValue() == ((int)ControlAEState.Precapture) || | ||
aeState.IntValue() == ((int)ControlAEState.FlashRequired)) | ||
{ | ||
Owner.mState = Camera2BasicFragment.STATE_WAITING_NON_PRECAPTURE; | ||
} | ||
break; | ||
} | ||
case Camera2BasicFragment.STATE_WAITING_NON_PRECAPTURE: | ||
{ | ||
// ControlAeState can be null on some devices | ||
Integer aeState = (Integer)result.Get(CaptureResult.ControlAeState); | ||
if (aeState == null || aeState.IntValue() != ((int)ControlAEState.Precapture)) | ||
{ | ||
Owner.mState = Camera2BasicFragment.STATE_PICTURE_TAKEN; | ||
Owner.CaptureStillPicture(); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
android5.0/Camera2Basic/Camera2Basic/Listeners/CameraCaptureSessionCallback.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
using Android.Hardware.Camera2; | ||
|
||
namespace Camera2Basic.Listeners | ||
{ | ||
public class CameraCaptureSessionCallback : CameraCaptureSession.StateCallback | ||
{ | ||
public Camera2BasicFragment Owner { get; set; } | ||
|
||
public CameraCaptureSessionCallback(Camera2BasicFragment owner) | ||
{ | ||
Owner = owner; | ||
} | ||
|
||
public override void OnConfigureFailed(CameraCaptureSession session) | ||
{ | ||
Owner.ShowToast("Failed"); | ||
} | ||
|
||
public override void OnConfigured(CameraCaptureSession session) | ||
{ | ||
// The camera is already closed | ||
if (null == Owner.mCameraDevice) | ||
{ | ||
return; | ||
} | ||
|
||
// When the session is ready, we start displaying the preview. | ||
Owner.mCaptureSession = session; | ||
try | ||
{ | ||
// Auto focus should be continuous for camera preview. | ||
Owner.mPreviewRequestBuilder.Set(CaptureRequest.ControlAfMode, (int)ControlAFMode.ContinuousPicture); | ||
// Flash is automatically enabled when necessary. | ||
Owner.SetAutoFlash(Owner.mPreviewRequestBuilder); | ||
|
||
// Finally, we start displaying the camera preview. | ||
Owner.mPreviewRequest = Owner.mPreviewRequestBuilder.Build(); | ||
Owner.mCaptureSession.SetRepeatingRequest(Owner.mPreviewRequest, | ||
Owner.mCaptureCallback, Owner.mBackgroundHandler); | ||
} | ||
catch (CameraAccessException e) | ||
{ | ||
e.PrintStackTrace(); | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
android5.0/Camera2Basic/Camera2Basic/Listeners/CameraCaptureStillPictureSessionCallback.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
using Android.Hardware.Camera2; | ||
using Android.Util; | ||
|
||
namespace Camera2Basic.Listeners | ||
{ | ||
public class CameraCaptureStillPictureSessionCallback : CameraCaptureSession.CaptureCallback | ||
{ | ||
private static readonly string TAG = "CameraCaptureStillPictureSessionCallback"; | ||
|
||
public Camera2BasicFragment Owner { get; set; } | ||
|
||
public CameraCaptureStillPictureSessionCallback(Camera2BasicFragment owner) | ||
{ | ||
Owner = owner; | ||
} | ||
|
||
public override void OnCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) | ||
{ | ||
Owner.ShowToast("Saved: " + Owner.mFile); | ||
Log.Debug(TAG, Owner.mFile.ToString()); | ||
Owner.UnlockFocus(); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
android5.0/Camera2Basic/Camera2Basic/Listeners/CameraStateListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
using Android.App; | ||
using Android.Hardware.Camera2; | ||
|
||
namespace Camera2Basic.Listeners | ||
{ | ||
public class CameraStateListener : CameraDevice.StateCallback | ||
{ | ||
public Camera2BasicFragment owner; | ||
public override void OnOpened(CameraDevice cameraDevice) | ||
{ | ||
// This method is called when the camera is opened. We start camera preview here. | ||
owner.mCameraOpenCloseLock.Release(); | ||
owner.mCameraDevice = cameraDevice; | ||
owner.CreateCameraPreviewSession(); | ||
} | ||
|
||
public override void OnDisconnected(CameraDevice cameraDevice) | ||
{ | ||
owner.mCameraOpenCloseLock.Release(); | ||
cameraDevice.Close(); | ||
owner.mCameraDevice = null; | ||
} | ||
|
||
public override void OnError(CameraDevice cameraDevice, CameraError error) | ||
{ | ||
owner.mCameraOpenCloseLock.Release(); | ||
cameraDevice.Close(); | ||
owner.mCameraDevice = null; | ||
if (owner == null) | ||
return; | ||
Activity activity = owner.Activity; | ||
if (activity != null) | ||
{ | ||
activity.Finish(); | ||
} | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.