public abstract class SimpleCamera2Activity
extends android.app.Activity
startCamera() inside your Activity's onCreate function.
To customize it's behavior override the following functions:
selectResolution(int, int, android.util.Size[])onCameraResolutionChange(int, int, int)configureCamera(android.hardware.camera2.CaptureRequest.Builder)selectCamera(java.lang.String, android.hardware.camera2.CameraCharacteristics)processFrame(android.media.Image)onCameraOpened(android.hardware.camera2.CameraDevice)onCameraDisconnected(android.hardware.camera2.CameraDevice)
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera2.full" />
| Modifier and Type | Class and Description |
|---|---|
protected static class |
SimpleCamera2Activity.CameraState |
| Modifier and Type | Field and Description |
|---|---|
protected float |
cameraToDisplayDensity |
protected android.util.DisplayMetrics |
displayMetrics |
protected android.view.TextureView |
mTextureView |
protected android.view.View |
mView |
protected boolean |
verbose |
protected int |
viewHeight |
protected int |
viewWidth |
DEFAULT_KEYS_DIALER, DEFAULT_KEYS_DISABLE, DEFAULT_KEYS_SEARCH_GLOBAL, DEFAULT_KEYS_SEARCH_LOCAL, DEFAULT_KEYS_SHORTCUT, FOCUSED_STATE_SET, RESULT_CANCELED, RESULT_FIRST_USER, RESULT_OKACCESSIBILITY_SERVICE, ACCOUNT_SERVICE, ACTIVITY_SERVICE, ALARM_SERVICE, APP_OPS_SERVICE, APPWIDGET_SERVICE, AUDIO_SERVICE, BATTERY_SERVICE, BIND_ABOVE_CLIENT, BIND_ADJUST_WITH_ACTIVITY, BIND_ALLOW_OOM_MANAGEMENT, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_EXTERNAL_SERVICE, BIND_IMPORTANT, BIND_NOT_FOREGROUND, BIND_WAIVE_PRIORITY, BLUETOOTH_SERVICE, CAMERA_SERVICE, CAPTIONING_SERVICE, CARRIER_CONFIG_SERVICE, CLIPBOARD_SERVICE, COMPANION_DEVICE_SERVICE, CONNECTIVITY_SERVICE, CONSUMER_IR_SERVICE, CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_RESTRICTED, DEVICE_POLICY_SERVICE, DISPLAY_SERVICE, DOWNLOAD_SERVICE, DROPBOX_SERVICE, FINGERPRINT_SERVICE, HARDWARE_PROPERTIES_SERVICE, INPUT_METHOD_SERVICE, INPUT_SERVICE, JOB_SCHEDULER_SERVICE, KEYGUARD_SERVICE, LAUNCHER_APPS_SERVICE, LAYOUT_INFLATER_SERVICE, LOCATION_SERVICE, MEDIA_PROJECTION_SERVICE, MEDIA_ROUTER_SERVICE, MEDIA_SESSION_SERVICE, MIDI_SERVICE, MODE_APPEND, MODE_ENABLE_WRITE_AHEAD_LOGGING, MODE_MULTI_PROCESS, MODE_NO_LOCALIZED_COLLATORS, MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, NETWORK_STATS_SERVICE, NFC_SERVICE, NOTIFICATION_SERVICE, NSD_SERVICE, POWER_SERVICE, PRINT_SERVICE, RECEIVER_VISIBLE_TO_INSTANT_APPS, RESTRICTIONS_SERVICE, SEARCH_SERVICE, SENSOR_SERVICE, SHORTCUT_SERVICE, STORAGE_SERVICE, STORAGE_STATS_SERVICE, SYSTEM_HEALTH_SERVICE, TELECOM_SERVICE, TELEPHONY_SERVICE, TELEPHONY_SUBSCRIPTION_SERVICE, TEXT_CLASSIFICATION_SERVICE, TEXT_SERVICES_MANAGER_SERVICE, TV_INPUT_SERVICE, UI_MODE_SERVICE, USAGE_STATS_SERVICE, USB_SERVICE, USER_SERVICE, VIBRATOR_SERVICE, WALLPAPER_SERVICE, WIFI_AWARE_SERVICE, WIFI_P2P_SERVICE, WIFI_SERVICE, WINDOW_SERVICE| Constructor and Description |
|---|
SimpleCamera2Activity() |
| Modifier and Type | Method and Description |
|---|---|
double[] |
cameraNominalFov()
Estimates the camera's horizontal and vertical FOV by picking a nominal value.
|
protected boolean |
closeCamera()
Closes the camera.
|
protected void |
configureCamera(android.hardware.camera2.CaptureRequest.Builder captureRequestBuilder)
Override to do custom configuration of the camera's settings.
|
protected boolean |
handleCameraOpenException(java.lang.Exception e)
An exception happened while trying to open the camera
|
protected boolean |
handleNoCameraSelected()
Called if no camera was selected when trying to open a camera
|
protected void |
handleOnImageAvailableException(java.lang.RuntimeException e)
An exception was thrown inside of OnImageAvailableListener.
|
protected void |
onCameraDisconnected(android.hardware.camera2.CameraDevice cameraDevice)
Invoked when the camera has been disconnected
|
protected void |
onCameraOpened(android.hardware.camera2.CameraDevice cameraDevice)
Invoked when the camera has been opened
|
protected void |
onCameraResolutionChange(int cameraWidth,
int cameraHeight,
int orientation)
Called when the camera's resolution has changed.
|
protected void |
onCreate(android.os.Bundle savedInstanceState) |
protected void |
onPause() |
protected void |
onResume() |
protected void |
openCamera(int widthTexture,
int heightTexture)
Tries to open a
CameraDevice. |
protected abstract void |
processFrame(android.media.Image image)
Process a single frame from the video feed.
|
protected void |
reopenCameraAtResolution(int cameraWidth,
int cameraHeight)
Re-opens the camera with the same settings at the specified resolution.
|
protected boolean |
selectCamera(java.lang.String id,
android.hardware.camera2.CameraCharacteristics characteristics)
By default this will select the backfacing camera.
|
protected int |
selectResolution(int widthTexture,
int heightTexture,
android.util.Size[] resolutions)
Selects the camera resolution from the list of possible values.
|
void |
setVerbose(boolean verbose) |
protected void |
startCamera() |
protected void |
startCameraTexture(android.view.TextureView view)
After this function is called the camera will be start.
|
protected void |
startCameraView(android.view.View view) |
addContentView, attachBaseContext, closeContextMenu, closeOptionsMenu, createPendingResult, dismissDialog, dismissKeyboardShortcutsHelper, dispatchGenericMotionEvent, dispatchKeyEvent, dispatchKeyShortcutEvent, dispatchPopulateAccessibilityEvent, dispatchTouchEvent, dispatchTrackballEvent, dump, enterPictureInPictureMode, enterPictureInPictureMode, findViewById, finish, finishActivity, finishActivityFromChild, finishAffinity, finishAfterTransition, finishAndRemoveTask, finishFromChild, getActionBar, getApplication, getCallingActivity, getCallingPackage, getChangingConfigurations, getComponentName, getContentScene, getContentTransitionManager, getCurrentFocus, getFragmentManager, getIntent, getLastNonConfigurationInstance, getLayoutInflater, getLoaderManager, getLocalClassName, getMaxNumPictureInPictureActions, getMediaController, getMenuInflater, getParent, getParentActivityIntent, getPreferences, getReferrer, getRequestedOrientation, getSearchEvent, getSystemService, getTaskId, getTitle, getTitleColor, getVoiceInteractor, getVolumeControlStream, getWindow, getWindowManager, hasWindowFocus, invalidateOptionsMenu, isActivityTransitionRunning, isChangingConfigurations, isChild, isDestroyed, isFinishing, isImmersive, isInMultiWindowMode, isInPictureInPictureMode, isLocalVoiceInteractionSupported, isTaskRoot, isVoiceInteraction, isVoiceInteractionRoot, managedQuery, moveTaskToBack, navigateUpTo, navigateUpToFromChild, onActionModeFinished, onActionModeStarted, onActivityReenter, onActivityResult, onApplyThemeResource, onAttachedToWindow, onAttachFragment, onBackPressed, onChildTitleChanged, onConfigurationChanged, onContentChanged, onContextItemSelected, onContextMenuClosed, onCreate, onCreateContextMenu, onCreateDescription, onCreateDialog, onCreateDialog, onCreateNavigateUpTaskStack, onCreateOptionsMenu, onCreatePanelMenu, onCreatePanelView, onCreateThumbnail, onCreateView, onCreateView, onDestroy, onDetachedFromWindow, onEnterAnimationComplete, onGenericMotionEvent, onKeyDown, onKeyLongPress, onKeyMultiple, onKeyShortcut, onKeyUp, onLocalVoiceInteractionStarted, onLocalVoiceInteractionStopped, onLowMemory, onMenuItemSelected, onMenuOpened, onMultiWindowModeChanged, onMultiWindowModeChanged, onNavigateUp, onNavigateUpFromChild, onNewIntent, onOptionsItemSelected, onOptionsMenuClosed, onPanelClosed, onPictureInPictureModeChanged, onPictureInPictureModeChanged, onPostCreate, onPostCreate, onPostResume, onPrepareDialog, onPrepareDialog, onPrepareNavigateUpTaskStack, onPrepareOptionsMenu, onPreparePanel, onProvideAssistContent, onProvideAssistData, onProvideKeyboardShortcuts, onProvideReferrer, onRequestPermissionsResult, onRestart, onRestoreInstanceState, onRestoreInstanceState, onRetainNonConfigurationInstance, onSaveInstanceState, onSaveInstanceState, onSearchRequested, onSearchRequested, onStart, onStateNotSaved, onStop, onTitleChanged, onTouchEvent, onTrackballEvent, onTrimMemory, onUserInteraction, onUserLeaveHint, onVisibleBehindCanceled, onWindowAttributesChanged, onWindowFocusChanged, onWindowStartingActionMode, onWindowStartingActionMode, openContextMenu, openOptionsMenu, overridePendingTransition, postponeEnterTransition, recreate, registerForContextMenu, releaseInstance, removeDialog, reportFullyDrawn, requestDragAndDropPermissions, requestPermissions, requestShowKeyboardShortcuts, requestVisibleBehind, requestWindowFeature, runOnUiThread, setActionBar, setContentTransitionManager, setContentView, setContentView, setContentView, setDefaultKeyMode, setEnterSharedElementCallback, setExitSharedElementCallback, setFeatureDrawable, setFeatureDrawableAlpha, setFeatureDrawableResource, setFeatureDrawableUri, setFinishOnTouchOutside, setImmersive, setIntent, setMediaController, setPictureInPictureParams, setProgress, setProgressBarIndeterminate, setProgressBarIndeterminateVisibility, setProgressBarVisibility, setRequestedOrientation, setResult, setResult, setSecondaryProgress, setShowWhenLocked, setTaskDescription, setTheme, setTitle, setTitle, setTitleColor, setTurnScreenOn, setVisible, setVolumeControlStream, setVrModeEnabled, shouldShowRequestPermissionRationale, shouldUpRecreateTask, showAssist, showDialog, showDialog, showLockTaskEscapeMessage, startActionMode, startActionMode, startActivities, startActivities, startActivity, startActivity, startActivityForResult, startActivityForResult, startActivityFromChild, startActivityFromChild, startActivityFromFragment, startActivityFromFragment, startActivityIfNeeded, startActivityIfNeeded, startIntentSender, startIntentSender, startIntentSenderForResult, startIntentSenderForResult, startIntentSenderFromChild, startIntentSenderFromChild, startLocalVoiceInteraction, startLockTask, startManagingCursor, startNextMatchingActivity, startNextMatchingActivity, startPostponedEnterTransition, startSearch, stopLocalVoiceInteraction, stopLockTask, stopManagingCursor, takeKeyEvents, triggerSearch, unregisterForContextMenuapplyOverrideConfiguration, getAssets, getResources, getThemebindService, checkCallingOrSelfPermission, checkCallingOrSelfUriPermission, checkCallingPermission, checkCallingUriPermission, checkPermission, checkSelfPermission, checkUriPermission, checkUriPermission, clearWallpaper, createConfigurationContext, createContextForSplit, createDeviceProtectedStorageContext, createDisplayContext, createPackageContext, databaseList, deleteDatabase, deleteFile, deleteSharedPreferences, enforceCallingOrSelfPermission, enforceCallingOrSelfUriPermission, enforceCallingPermission, enforceCallingUriPermission, enforcePermission, enforceUriPermission, enforceUriPermission, fileList, getApplicationContext, getApplicationInfo, getBaseContext, getCacheDir, getClassLoader, getCodeCacheDir, getContentResolver, getDatabasePath, getDataDir, getDir, getExternalCacheDir, getExternalCacheDirs, getExternalFilesDir, getExternalFilesDirs, getExternalMediaDirs, getFilesDir, getFileStreamPath, getMainLooper, getNoBackupFilesDir, getObbDir, getObbDirs, getPackageCodePath, getPackageManager, getPackageName, getPackageResourcePath, getSharedPreferences, getSystemServiceName, getWallpaper, getWallpaperDesiredMinimumHeight, getWallpaperDesiredMinimumWidth, grantUriPermission, isDeviceProtectedStorage, isRestricted, moveDatabaseFrom, moveSharedPreferencesFrom, openFileInput, openFileOutput, openOrCreateDatabase, openOrCreateDatabase, peekWallpaper, registerReceiver, registerReceiver, registerReceiver, registerReceiver, removeStickyBroadcast, removeStickyBroadcastAsUser, revokeUriPermission, revokeUriPermission, sendBroadcast, sendBroadcast, sendBroadcastAsUser, sendBroadcastAsUser, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcastAsUser, sendStickyBroadcast, sendStickyBroadcastAsUser, sendStickyOrderedBroadcast, sendStickyOrderedBroadcastAsUser, setWallpaper, setWallpaper, startForegroundService, startInstrumentation, startService, stopService, unbindService, unregisterReceivergetColor, getColorStateList, getDrawable, getString, getString, getSystemService, getText, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, registerComponentCallbacks, unregisterComponentCallbacksprotected android.view.TextureView mTextureView
protected android.view.View mView
protected int viewWidth
protected int viewHeight
protected float cameraToDisplayDensity
protected boolean verbose
protected android.util.DisplayMetrics displayMetrics
protected void startCameraTexture(android.view.TextureView view)
view - The view the camera is displayed inside or null if not displayedprotected void startCameraView(android.view.View view)
protected void startCamera()
protected void onCreate(android.os.Bundle savedInstanceState)
onCreate in class android.app.Activityprotected void onResume()
onResume in class android.app.Activityprotected void onPause()
onPause in class android.app.Activityprotected int selectResolution(int widthTexture,
int heightTexture,
android.util.Size[] resolutions)
widthTexture - Width of the texture the preview is displayed inside of. <= 0 if no viewheightTexture - Height of the texture the preview is displayed inside of. <= 0 if no viewresolutions - array of possible resolutionsprotected void onCameraResolutionChange(int cameraWidth,
int cameraHeight,
int orientation)
protected void configureCamera(android.hardware.camera2.CaptureRequest.Builder captureRequestBuilder)
captureRequestBuilder - used to configure the cameraprotected boolean selectCamera(java.lang.String id,
android.hardware.camera2.CameraCharacteristics characteristics)
protected abstract void processFrame(android.media.Image image)
protected void openCamera(int widthTexture,
int heightTexture)
CameraDevice. The result is listened by `mStateCallback`.protected boolean handleNoCameraSelected()
protected boolean handleCameraOpenException(java.lang.Exception e)
protected void reopenCameraAtResolution(int cameraWidth,
int cameraHeight)
protected boolean closeCamera()
protected void onCameraOpened(@NonNull
android.hardware.camera2.CameraDevice cameraDevice)
protected void onCameraDisconnected(@NonNull
android.hardware.camera2.CameraDevice cameraDevice)
public double[] cameraNominalFov()
protected void handleOnImageAvailableException(java.lang.RuntimeException e)
public void setVerbose(boolean verbose)