如何在默认相机应用程序上添加叠加视图,如剪影



所以我目前正在开发一个应用程序,该应用程序具有在相机预览中叠加视图或图像的功能。我试过这段代码:

btn_Capture.setOnClickListener {
//if system os is Marshmallow or Above, we need to request runtime permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (checkSelfPermission(Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED ||
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_DENIED){
//permission was not enabled
val permission = arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
//show popup to request permission
requestPermissions(permission, PERMISSION_CODE)
}
else{
//permission already granted
showDialog()
//openCamera()
}
}
else{
//system os is < marshmallow
showDialog()
//openCamera()
}
}

这是我的showDialog函数:

private fun showDialog(){
val listItems = arrayOf("3 meters", "4 meters", "5 meters", "6 meters", "7 meters", "8 meters", "9 meters", "10 meters", "11 meters")
val builder = AlertDialog.Builder(this@MainActivity)
builder.setTitle("Choose distance of viewport")
val checkedItem = 0 //this will checked the item when user open the dialog
builder.setSingleChoiceItems(
listItems, checkedItem
) { dialog, which ->
SelectedRadio = listItems[which]
}
builder.setPositiveButton(
"Go"
) { dialog, which -> dialog.dismiss()
Toast.makeText(this@MainActivity,SelectedRadio,Toast.LENGTH_LONG).show()
openCamera()
}
val dialog = builder.create()
dialog.show()
}

所以我想每次打开相机应用程序时都叠加一个图像。我还尝试创建自定义布局并将其附加到相机的父级,但它没有按照我想要的方式工作。

所以我这样做了

安卓清单

<?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>

最新更新