我是android开发的新手,正在开发kotlin应用程序。我想在拍照或录制视频时显示当前日期和时间,并在拍摄/录制后将时间戳与图像一起保存。我所有的密码都在Kotlin。。有人能帮我写代码吗。下面提到的是我的代码。
主要活动
class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener {
private val FADE_DELAY = 5000L
private val CAPTURE_ANIMATION_DURATION = 100L
lateinit var mTimerHandler: Handler
private lateinit var mOrientationEventListener: OrientationEventListener
private lateinit var mFocusCircleView: FocusCircleView
private lateinit var mFadeHandler: Handler
private lateinit var mCameraImpl: MyCameraImpl
private var mPreview: MyPreview? = null
private var mPreviewUri: Uri? = null
private var mIsInPhotoMode = false
private var mIsCameraAvailable = false
private var mIsVideoCaptureIntent = false
private var mIsHardwareShutterHandled = false
private var mCurrVideoRecTimer = 0
var mLastHandledOrientation = 0
override fun onCreate(savedInstanceState: Bundle?) {
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON or
WindowManager.LayoutParams.FLAG_FULLSCREEN)
useDynamicTheme = false
super.onCreate(savedInstanceState)
appLaunched(BuildConfig.APPLICATION_ID)
requestWindowFeature(Window.FEATURE_NO_TITLE)
initVariables()
tryInitCamera()
supportActionBar?.hide()
checkWhatsNewDialog()
setupOrientationEventListener()
}
override fun onResume() {
super.onResume()
if (hasStorageAndCameraPermissions()) {
mPreview?.onResumed()
resumeCameraItems()
setupPreviewImage(mIsInPhotoMode)
scheduleFadeOut()
mFocusCircleView.setStrokeColor(getAdjustedPrimaryColor())
if (mIsVideoCaptureIntent && mIsInPhotoMode) {
handleTogglePhotoVideo()
checkButtons()
}
toggleBottomButtons(false)
}
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
if (hasStorageAndCameraPermissions()) {
mOrientationEventListener.enable()
}
}
override fun onPause() {
super.onPause()
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
if (!hasStorageAndCameraPermissions() || isAskingPermissions) {
return
}
mFadeHandler.removeCallbacksAndMessages(null)
hideTimer()
mOrientationEventListener.disable()
if (mPreview?.getCameraState() == STATE_PICTURE_TAKEN) {
toast(R.string.photo_not_saved)
}
ensureBackgroundThread {
mPreview?.onPaused()
}
}
override fun onDestroy() {
super.onDestroy()
mPreview = null
}
private fun initVariables() {
mIsInPhotoMode = config.initPhotoMode
mIsCameraAvailable = false
mIsVideoCaptureIntent = false
mIsHardwareShutterHandled = false
mCurrVideoRecTimer = 0
mLastHandledOrientation = 0
mCameraImpl = MyCameraImpl(applicationContext)
if (config.alwaysOpenBackCamera) {
config.lastUsedCamera = mCameraImpl.getBackCameraId().toString()
}
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
return if (keyCode == KeyEvent.KEYCODE_CAMERA && !mIsHardwareShutterHandled) {
mIsHardwareShutterHandled = true
shutterPressed()
true
} else if (!mIsHardwareShutterHandled && config.volumeButtonsAsShutter && (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
mIsHardwareShutterHandled = true
shutterPressed()
true
} else {
super.onKeyDown(keyCode, event)
}
}
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
mIsHardwareShutterHandled = false
}
return super.onKeyUp(keyCode, event)
}
private fun hideIntentButtons() {
toggle_photo_video.beGone()
settings.beGone()
last_photo_video_preview.beGone()
}
private fun tryInitCamera() {
handlePermission(PERMISSION_CAMERA) {
if (it) {
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
initializeCamera()
} else {
toast(R.string.no_storage_permissions)
finish()
}
}
} else {
toast(R.string.no_camera_permissions)
finish()
}
}
}
private fun isImageCaptureIntent() = intent?.action == MediaStore.ACTION_IMAGE_CAPTURE || intent?.action == MediaStore.ACTION_IMAGE_CAPTURE_SECURE
private fun checkImageCaptureIntent() {
if (isImageCaptureIntent()) {
hideIntentButtons()
val output = intent.extras?.get(MediaStore.EXTRA_OUTPUT)
if (output != null && output is Uri) {
mPreview?.setTargetUri(output)
}
}
}
private fun checkVideoCaptureIntent() {
if (intent?.action == MediaStore.ACTION_VIDEO_CAPTURE) {
mIsVideoCaptureIntent = true
mIsInPhotoMode = false
hideIntentButtons()
shutter.setImageResource(R.drawable.ic_video_rec)
}
}
private fun initializeCamera() {
setContentView(R.layout.activity_main)
initButtons()
(btn_holder.layoutParams as RelativeLayout.LayoutParams).setMargins(0, 0, 0, (navigationBarHeight + resources.getDimension(R.dimen.activity_margin)).toInt())
checkVideoCaptureIntent()
mPreview = CameraPreview(this, camera_texture_view, mIsInPhotoMode)
view_holder.addView(mPreview as ViewGroup)
checkImageCaptureIntent()
mPreview?.setIsImageCaptureIntent(isImageCaptureIntent())
val imageDrawable = if (config.lastUsedCamera == mCameraImpl.getBackCameraId().toString()) R.drawable.ic_camera_front_vector else R.drawable.ic_camera_rear_vector
toggle_camera.setImageResource(imageDrawable)
mFocusCircleView = FocusCircleView(applicationContext)
view_holder.addView(mFocusCircleView)
mTimerHandler = Handler()
mFadeHandler = Handler()
setupPreviewImage(true)
val initialFlashlightState = if (config.turnFlashOffAtStartup) FLASH_OFF else config.flashlightState
mPreview!!.setFlashlightState(initialFlashlightState)
updateFlashlightState(initialFlashlightState)
}
private fun initButtons() {
toggle_camera.setOnClickListener { toggleCamera() }
last_photo_video_preview.setOnClickListener { showLastMediaPreview() }
toggle_flash.setOnClickListener { toggleFlash() }
shutter.setOnClickListener { shutterPressed() }
settings.setOnClickListener { launchSettings() }
toggle_photo_video.setOnClickListener { handleTogglePhotoVideo() }
change_resolution.setOnClickListener { mPreview?.showChangeResolutionDialog() }
}
private fun toggleCamera() {
if (checkCameraAvailable()) {
mPreview!!.toggleFrontBackCamera()
}
}
private fun showLastMediaPreview() {
if (mPreviewUri != null) {
val path = applicationContext.getRealPathFromURI(mPreviewUri!!) ?: mPreviewUri!!.toString()
openPathIntent(path, false, BuildConfig.APPLICATION_ID)
}
}
private fun toggleFlash() {
if (checkCameraAvailable()) {
mPreview?.toggleFlashlight()
}
}
fun updateFlashlightState(state: Int) {
config.flashlightState = state
val flashDrawable = when (state) {
FLASH_OFF -> R.drawable.ic_flash_off_vector
FLASH_ON -> R.drawable.ic_flash_on_vector
else -> R.drawable.ic_flash_auto_vector
}
toggle_flash.setImageResource(flashDrawable)
}
fun updateCameraIcon(isUsingFrontCamera: Boolean) {
toggle_camera.setImageResource(if (isUsingFrontCamera) R.drawable.ic_camera_rear_vector else R.drawable.ic_camera_front_vector)
}
private fun shutterPressed() {
if (checkCameraAvailable()) {
handleShutter()
}
}
private fun handleShutter() {
if (mIsInPhotoMode) {
toggleBottomButtons(true)
mPreview?.tryTakePicture()
capture_black_screen.animate().alpha(0.8f).setDuration(CAPTURE_ANIMATION_DURATION).withEndAction {
capture_black_screen.animate().alpha(0f).setDuration(CAPTURE_ANIMATION_DURATION).start()
}.start()
} else {
mPreview?.toggleRecording()
}
}
fun toggleBottomButtons(hide: Boolean) {
runOnUiThread {
val alpha = if (hide) 0f else 1f
shutter.animate().alpha(alpha).start()
toggle_camera.animate().alpha(alpha).start()
toggle_flash.animate().alpha(alpha).start()
shutter.isClickable = !hide
toggle_camera.isClickable = !hide
toggle_flash.isClickable = !hide
}
}
private fun launchSettings() {
if (settings.alpha == 1f) {
val intent = Intent(applicationContext, SettingsActivity::class.java)
startActivity(intent)
} else {
fadeInButtons()
}
}
private fun handleTogglePhotoVideo() {
handlePermission(PERMISSION_RECORD_AUDIO) {
if (it) {
togglePhotoVideo()
} else {
toast(R.string.no_audio_permissions)
if (mIsVideoCaptureIntent) {
finish()
}
}
}
}
private fun togglePhotoVideo() {
if (!checkCameraAvailable()) {
return
}
if (mIsVideoCaptureIntent) {
mPreview?.tryInitVideoMode()
}
mPreview?.setFlashlightState(FLASH_OFF)
hideTimer()
mIsInPhotoMode = !mIsInPhotoMode
config.initPhotoMode = mIsInPhotoMode
showToggleCameraIfNeeded()
checkButtons()
toggleBottomButtons(false)
}
private fun checkButtons() {
if (mIsInPhotoMode) {
initPhotoMode()
} else {
tryInitVideoMode()
}
}
private fun initPhotoMode() {
toggle_photo_video.setImageResource(R.drawable.ic_video_vector)
shutter.setImageResource(R.drawable.ic_shutter_vector)
mPreview?.initPhotoMode()
setupPreviewImage(true)
}
private fun tryInitVideoMode() {
if (mPreview?.initVideoMode() == true) {
initVideoButtons()
} else {
if (!mIsVideoCaptureIntent) {
toast(R.string.video_mode_error)
}
}
}
private fun initVideoButtons() {
toggle_photo_video.setImageResource(R.drawable.ic_camera_vector)
showToggleCameraIfNeeded()
shutter.setImageResource(R.drawable.ic_video_rec)
setupPreviewImage(false)
mPreview?.checkFlashlight()
}
private fun setupPreviewImage(isPhoto: Boolean) {
val uri = if (isPhoto) MediaStore.Images.Media.EXTERNAL_CONTENT_URI else MediaStore.Video.Media.EXTERNAL_CONTENT_URI
val lastMediaId = getLatestMediaId(uri)
if (lastMediaId == 0L) {
return
}
mPreviewUri = Uri.withAppendedPath(uri, lastMediaId.toString())
runOnUiThread {
if (!isDestroyed) {
val options = RequestOptions()
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.NONE)
Glide.with(this)
.load(mPreviewUri)
.apply(options)
.transition(DrawableTransitionOptions.withCrossFade())
.into(last_photo_video_preview)
}
}
}
private fun scheduleFadeOut() {
if (!config.keepSettingsVisible) {
mFadeHandler.postDelayed({
fadeOutButtons()
}, FADE_DELAY)
}
}
private fun fadeOutButtons() {
fadeAnim(settings, .5f)
fadeAnim(toggle_photo_video, .0f)
fadeAnim(change_resolution, .0f)
fadeAnim(last_photo_video_preview, .0f)
}
private fun fadeInButtons() {
fadeAnim(settings, 1f)
fadeAnim(toggle_photo_video, 1f)
fadeAnim(change_resolution, 1f)
fadeAnim(last_photo_video_preview, 1f)
scheduleFadeOut()
}
private fun fadeAnim(view: View, value: Float) {
view.animate().alpha(value).start()
view.isClickable = value != .0f
}
private fun hideNavigationBarIcons() {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE
}
private fun showTimer() {
video_rec_curr_timer.beVisible()
setupTimer()
}
private fun hideTimer() {
video_rec_curr_timer.text = 0.getFormattedDuration()
video_rec_curr_timer.beGone()
mCurrVideoRecTimer = 0
mTimerHandler.removeCallbacksAndMessages(null)
}
private fun setupTimer() {
runOnUiThread(object : Runnable {
override fun run() {
video_rec_curr_timer.text = mCurrVideoRecTimer++.getFormattedDuration()
mTimerHandler.postDelayed(this, 1000L)
}
})
}
private fun resumeCameraItems() {
showToggleCameraIfNeeded()
hideNavigationBarIcons()
if (!mIsInPhotoMode) {
initVideoButtons()
}
}
private fun showToggleCameraIfNeeded() {
toggle_camera?.beInvisibleIf(mCameraImpl.getCountOfCameras() ?: 1 <= 1)
}
private fun hasStorageAndCameraPermissions() = hasPermission(PERMISSION_WRITE_STORAGE) && hasPermission(PERMISSION_CAMERA)
private fun setupOrientationEventListener() {
mOrientationEventListener = object : OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
override fun onOrientationChanged(orientation: Int) {
if (isDestroyed) {
mOrientationEventListener.disable()
return
}
val currOrient = when (orientation) {
in 75..134 -> ORIENT_LANDSCAPE_RIGHT
in 225..289 -> ORIENT_LANDSCAPE_LEFT
else -> ORIENT_PORTRAIT
}
if (currOrient != mLastHandledOrientation) {
val degrees = when (currOrient) {
ORIENT_LANDSCAPE_LEFT -> 90
ORIENT_LANDSCAPE_RIGHT -> -90
else -> 0
}
animateViews(degrees)
mLastHandledOrientation = currOrient
}
}
}
}
private fun animateViews(degrees: Int) {
val views = arrayOf<View>(toggle_camera, toggle_flash, toggle_photo_video, change_resolution, shutter, settings, last_photo_video_preview)
for (view in views) {
rotate(view, degrees)
}
}
private fun rotate(view: View, degrees: Int) = view.animate().rotation(degrees.toFloat()).start()
private fun checkCameraAvailable(): Boolean {
if (!mIsCameraAvailable) {
toast(R.string.camera_unavailable)
}
return mIsCameraAvailable
}
fun setFlashAvailable(available: Boolean) {
if (available) {
toggle_flash.beVisible()
} else {
toggle_flash.beInvisible()
toggle_flash.setImageResource(R.drawable.ic_flash_off_vector)
mPreview?.setFlashlightState(FLASH_OFF)
}
}
fun setIsCameraAvailable(available: Boolean) {
mIsCameraAvailable = available
}
fun setRecordingState(isRecording: Boolean) {
runOnUiThread {
if (isRecording) {
shutter.setImageResource(R.drawable.ic_video_stop)
toggle_camera.beInvisible()
showTimer()
} else {
shutter.setImageResource(R.drawable.ic_video_rec)
showToggleCameraIfNeeded()
hideTimer()
}
}
}
fun videoSaved(uri: Uri) {
setupPreviewImage(false)
if (mIsVideoCaptureIntent) {
Intent().apply {
data = uri
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
setResult(Activity.RESULT_OK, this)
}
finish()
}
}
fun drawFocusCircle(x: Float, y: Float) = mFocusCircleView.drawFocusCircle(x, y)
override fun mediaSaved(path: String) {
rescanPaths(arrayListOf(path)) {
setupPreviewImage(true)
Intent(BROADCAST_REFRESH_MEDIA).apply {
putExtra(REFRESH_PATH, path)
`package` = "com.simplemobiletools.gallery"
sendBroadcast(this)
}
}
if (isImageCaptureIntent()) {
setResult(Activity.RESULT_OK)
finish()
}
}
private fun checkWhatsNewDialog() {
arrayListOf<Release>().apply {
add(Release(33, R.string.release_33))
add(Release(35, R.string.release_35))
add(Release(39, R.string.release_39))
add(Release(44, R.string.release_44))
add(Release(46, R.string.release_46))
add(Release(52, R.string.release_52))
checkWhatsNew(this, BuildConfig.VERSION_CODE)
}
}
}
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<AutoFitTextureView
android:id="@+id/camera_texture_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#000"
android:layout_gravity="bottom|center_vertical|center_horizontal|center"
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"/>
<ImageView
android:id="@+id/capture_black_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"
android:background="#000000" />
<ImageView
android:id="@+id/settings"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_alignParentRight="true"
android:padding="@dimen/normal_margin"
android:src="@drawable/ic_settings_cog"/>
<ImageView
android:id="@+id/toggle_photo_video"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_below="@+id/settings"
android:layout_alignParentRight="true"
android:padding="@dimen/normal_margin"
android:src="@drawable/ic_video_vector"/>
<ImageView
android:id="@+id/change_resolution"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_below="@+id/toggle_photo_video"
android:layout_alignParentRight="true"
android:padding="@dimen/normal_margin"
android:src="@drawable/ic_resolution_vector"/>
<ImageView
android:id="@+id/last_photo_video_preview"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_below="@+id/change_resolution"
android:layout_alignParentRight="true"
android:padding="@dimen/medium_margin"/>
<LinearLayout
android:id="@+id/btn_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal|bottom">
<ImageView
android:id="@+id/toggle_camera"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_weight="1"
android:padding="@dimen/normal_margin"
android:src="@drawable/ic_camera_front_vector"/>
<ImageView
android:id="@+id/shutter"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_weight="1"
android:src="@drawable/ic_shutter_vector"/>
<ImageView
android:id="@+id/toggle_flash"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_weight="1"
android:padding="@dimen/normal_margin"
android:src="@drawable/ic_flash_off_vector"/>
</LinearLayout>
<TextView
android:id="@+id/video_rec_curr_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_holder"
android:layout_centerHorizontal="true"
android:text="00:00"
android:textColor="@android:color/white"
android:visibility="gone"/>
</RelativeLayout>
暂无答案!
目前还没有任何答案,快来回答吧!