1 Introduction
During this period, I am usingnatario1/CameraView to implement filtered 预览
, a>, but the author has not fixed this problem. , there are many mentions of this in When taking photos with more than one filter, the preview will be normal, but the photos taken will be completely black. filters, it is normal to superimpose , superimposing Especially for taking pictures using has entered the deep water area, and it gradually appears that it cannot meet our needs. However, as the project continues to deepen, the use of the packaging is quite in place, it indeed saved us a lot of time in the early stages of the project. Since function. 拍照
, 录像
CameraView
CameraView
MultiFilter
2
2
Github
issues
BUG
First, let’s clarifyCameraView
how the filter is called, and also let us clarify the general situation of the code when encountering this problem, so as to reproduce this BUG
.
2. Preliminary operation
Create a new projectAndroid
, Activity
set to landscape mode, make sure to add camera related permissions, and apply for permissions, depend on < a i=3>’s dependent librariesCameraView
implementation("com.otaliastudios:cameraview:2.7.2")
3. Write XML layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyMainActivity">
<com.otaliastudios.cameraview.CameraView
android:id="@+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cameraFacing="front"
app:cameraEngine="camera2"
app:cameraExperimental="true"
app:cameraMode="video" />
<ImageView
android:id="@+id/img"
android:layout_width="300dp"
android:layout_height="200dp" />
<Button
android:id="@+id/btn_take_picture"
android:layout_gravity="right|bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="拍照" />
</FrameLayout>
4. Initialize CameraView and add filters
binding.cameraView.setLifecycleOwner(this)
val multiFilter = MultiFilter()
val contrastFilter = ContrastFilter()
contrastFilter.contrast = 1.05F
multiFilter.addFilter(contrastFilter)
val brightnessFilter = Filters.BRIGHTNESS.newInstance() as BrightnessFilter
brightnessFilter.brightness = 1.2F
multiFilter.addFilter(brightnessFilter)
val saturationFilter = Filters.SATURATION.newInstance() as SaturationFilter
saturationFilter.saturation = 1F
multiFilter.addFilter(saturationFilter)
binding.cameraView.filter = multiFilter
5. Take photos
binding.btnTakePicture.setOnClickListener {
//带滤镜拍照
binding.cameraView.takePictureSnapshot()
}
binding.cameraView.addCameraListener(object : CameraListener() {
override fun onPictureTaken(result: PictureResult) {
super.onPictureTaken(result)
//拍照回调
val bitmap = BitmapFactory.decodeByteArray(result.data, 0, result.data.size)
bitmap?.also {
Toast.makeText(this@Test2Activity, "拍照成功", Toast.LENGTH_SHORT).show()
//将Bitmap设置到ImageView上
binding.img.setImageBitmap(it)
val file = getNewImageFile()
//保存图片到指定目录
ImageUtils.save(it, file, Bitmap.CompressFormat.JPEG)
}
}
})
6. Run the program
Run the program and click to take a photo, and we can reproduce thisBUG
: The picture in the upper left corner is completely black after taking the picture.
7. Others
7.1 CameraView source code analysis series
Android Camera Library CameraView Source Code Analysis (1): Preview-CSDN Blog
Android Camera Library CameraView Source Code Analysis (2): Taking Photos-CSDN Blog
Android camera library CameraView source code analysis (3): Filter related class description-CSDN Blog
Android camera library CameraView source code analysis (4): Taking photos with filters-CSDN Blog< a i=4>Android camera library CameraView source code analysis (5): Saving filter effects-CSDN Blog
7.2 Solve CameraViewBUG
Android solves the BUG of a black screen when taking pictures with CameraView superimposed with more than 2 filters (1): Recurring BUG-CSDN Blog
Android solves the BUG of a black screen when taking pictures with CameraView superimposed with more than 2 filters (2): Solving the BUG-CSDN Blog
Why are the preview and photo effects of the camera library CameraView inconsistent?-CSDN Blog