Итак, я сделал это следующим образом
Манифест Android
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@drawable/temaiconfinalllll"
android:label="@string/app_name"
android:roundIcon="@drawable/temaiconfinalllll"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".CustomCamera2"
android:hardwareAccelerated="true"
android:label="CustomCamera2"
android:screenOrientation="landscape"
android:theme="@style/AppTheme.NoActionBar"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Это моя пользовательская активность камеры
class CustomCamera2 : AppCompatActivity() {
lateinit var newView: ImageView
var pattern = "yyyy-MM-dd HH:mm:ss"
var simpleDateFormat = SimpleDateFormat(pattern)
var date = simpleDateFormat.format(Date())
var fotoapparat: Fotoapparat? = null
var filename = date + "Tema.png"
val sd = Environment.getExternalStorageDirectory()
val finalsd = sd.toString() + "/Pictures"
var dest = File(finalsd, filename)
var fotoapparatState : FotoapparatState? = null
var cameraStatus : CameraState? = null
var flashState: FlashState? = null
val permissions = arrayOf(android.Manifest.permission.CAMERA, android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.content_custom_camera2)
createFotoapparat()
cameraStatus = CameraState.BACK
flashState = FlashState.OFF
fotoapparatState = FotoapparatState.OFF
myview.setLayerType(View.LAYER_TYPE_SOFTWARE,null)
fab_camera.setOnClickListener {
takePhoto()
}
fab_switch_camera.setOnClickListener {
switchCamera()
}
fab_flash.setOnClickListener {
changeFlashState()
}
}
private fun createFotoapparat(){
val cameraView = findViewById<CameraView>(R.id.camera_view)
fotoapparat = Fotoapparat(
context = this,
view = cameraView,
scaleType = ScaleType.CenterCrop,
lensPosition = back(),
logger = loggers(
logcat()
),
cameraErrorCallback = { error ->
println("Recorder errors: $error")
}
)
}
private fun changeFlashState() {
fotoapparat?.updateConfiguration(
CameraConfiguration(
flashMode = if(flashState == FlashState.TORCH) off() else torch()
)
)
if(flashState == FlashState.TORCH) flashState = FlashState.OFF
else flashState = FlashState.TORCH
}
private fun switchCamera() {
fotoapparat?.switchTo(
lensPosition = if (cameraStatus == CameraState.BACK) front() else back(),
cameraConfiguration = CameraConfiguration()
)
if(cameraStatus == CameraState.BACK) cameraStatus = CameraState.FRONT
else cameraStatus = CameraState.BACK
}
private fun takePhoto() {
if (hasNoPermissions()) {
requestPermission()
}else{
fotoapparat
?.takePicture()
?.saveToFile(dest)
Handler().postDelayed(
{
StartEdit()
},
1300 // value in milliseconds
)
}
}
fun StartEdit(){
val dest2:String = dest.absolutePath.toString()
val intent = Intent(this,EditActivity2::class.java)
intent.putExtra("ImagePath", dest2)
startActivity(intent)
Toast.makeText(this,dest.toString(),Toast.LENGTH_LONG).show()
pattern = "yyyy-MM-dd HH:mm:ss"
simpleDateFormat = SimpleDateFormat(pattern)
date = simpleDateFormat.format(Date())
filename = date + "Tema.png"
dest = File(finalsd, filename)
}
override fun onStart() {
super.onStart()
if (hasNoPermissions()) {
requestPermission()
}else{
fotoapparat?.start()
fotoapparatState = FotoapparatState.ON
}
}
private fun hasNoPermissions(): Boolean{
return ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
}
fun requestPermission(){
ActivityCompat.requestPermissions(this, permissions,0)
}
override fun onStop() {
super.onStop()
fotoapparat?.stop()
FotoapparatState.OFF
}
override fun onResume() {
super.onResume()
if(!hasNoPermissions() && fotoapparatState == FotoapparatState.OFF){
val intent = Intent(baseContext, MainActivity::class.java)
startActivity(intent)
finish()
}
}
enum class CameraState{
FRONT, BACK
}
enum class FlashState{
TORCH, OFF
}
enum class FotoapparatState{
ON, OFF
}
}
Тогда это мой XML-файл
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
android:background="@android:color/black">
<io.fotoapparat.view.CameraView
android:id="@+id/camera_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
android:src="@drawable/camera"
android:layout_alignParentBottom="true"
android:layout_margin="32dp"
android:layout_centerHorizontal="true"
app:backgroundTint="@android:color/white"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_flash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
android:src="@drawable/torch"
android:layout_alignParentBottom="true"
android:layout_margin="32dp"
app:backgroundTint="@android:color/white"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_switch_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
android:src="@drawable/palit"
android:layout_alignParentBottom="true"
android:layout_margin="32dp"
android:layout_alignParentRight="true"
app:backgroundTint="@android:color/white"/>
<!--<com.google.android.material.transformation.TransformationChildLayout
android:id="@+id/Hello"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/guidethree"/>-->
<ImageView
android:id="@+id/myview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/guidethree"/>
</RelativeLayout>