一、调用系统camera
btn_camera.setOnClickListener{
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivity(intent)
startActivityForResult(intent, requestCode:1)
}
二、获取返回结果,显示压缩图
此方法显示的图片为压缩图
override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent?){
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == RESULT_OK)
{
if(requestCode == 1)
{
val bitmap = data?.extras?.get("data") as Bitmap
image.setImageBitmap(bitmap)
}
}
}
三、完整显示图片
- 修改setOnClickListener
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val photoUri = FileProvider.getUriForFile(this, "Camera.fileProvider", File("$path/test123.png"))
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
startActivityForResult(intent, 2)
- 修改onActivityResult
override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent)
{
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == RESULT_OK)
{
if(requestCode == 1)
{
......
}
else if(requestCode == 2)
{
val fis = FileInputStream(path)
val bitMap = BitmapFactory.decodeStream(fis)
image.setImageBitmap(bitMap)
fis.close()
val pUri = Uri.fromFile(File(path))
image.setImageURI(pUri)
}
}
}
此方法需注意图片文件是否创建成功,创建不成功时可能出现异常。 对于高版本的Android系统。无法通过原始的Uri.formFile函数获取Uri。所以需要通过FileProvider来获取。具体操作方法可以参考通过File Provider获取Uri
四、自定义相机
1. 创建自定义相机类
class MyCameraActivity:Activity(){
private lateinit var mCamera:Camera
private lateinit var mHolder:SurfaceHolder
override fun onCreate(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.my_camera)
mHolder = my_camera_view.getHolder()
mHolder = addCallback(this)
}
}
1.1 获取系统相机。
private fun getCamera():Camera{
return Camera.open()
}
1.2 实时显示相机内容。
fun startAction(camera:Camera, holder:SurfaceHolder){
camera.setPreviewDisplay(holder)
camera.setDisplayOrientation(90)
camera.startPreview()
}
1.3 释放相机资源
fun releaseCamera(){
if(mCamera != null){
mCamera.setPreviewCallback(null)
mCamera.stopPreview()
mCamera.release()
mCamera = null
}
}
1.4 camera 生命周期绑定
override fun onResume(){
super.onResume()
if(mCamera == null)
mCamera = getCamera()
if(mHolder != null)
{
startAction(mCamera, mHolder)
}
}
override fun onPause(){
super.onPause()
releaseCamera()
}
1.5 实现SurfaceHolder.Callback接口
override fun surfaceCreated(holder:SurfaceHolder){
startAction(mCamera, mHolder)
}
override fun surfaceChanged(holder:SurfaceHolder, format:Int, width:Int, height:Int){
mCamera.stopPreview()
startAction(mCamera, mHolder)
}
override fun surfaceDestroyed(holder:SurfaceHolder){
releaseCamera()
}
1.6 实现相机预览时点击预览界面相机自动对焦功能
my_camera_view.setOnClickListener{mCamera!!.autoFocus(null)}
2. 添加Camera使用权限
AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA"/>
<activity android:name=".MyCameraClass"/>
3. 主页面添加按钮打开myCamera页面
MainActivity.kt
mycamerabtn.setOnClickListener{
startActivity(Intent(this, MyCameraClass::class.java))
}
4. 添加拍照响应函数
btn_my_camera.setOnClickListener{
var parameters = mCamera!!.parameters
parameters.pictureFormat = ImageFormat.JPEG
parameters.setPictureSize(800, 400)
parameters.focusMode = Camera.Parameters.FOCUS_MODE_AUTO
mCamera!!.autoFocus(object:Camera.AutoFocusCallback{
override fun onAutoFocus(success:Boolean, camera:Camera?){
mCamera!!.takePicture(null, null, object:Camera.PictureCallback{
override fun onPictureTaken(data:ByteArray?, camera:Camera?){
if(success){
File("/sdcard/Pictures/temp.png").writeBytes(data!!)
this@MyCameraActivity.finish()
}
})
}
}
})
}
5. 添加拍照后显示界面
5.1 创建拍照显示页面布局
<ImageView
android:id="@+id/pic_show"
android:layout_width="mactch_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="back"
android:id="@+id/btn_back"/>
5.2 创建拍照显示页面类
class resultActivity:Activity(){
override fun onCreate(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.picture_show)
val picPath = intent.getStringExtra("picPath")
val fis = FileInputStream(picPath)
var bitmap = BitmapFactory.decodeStream(fis)
val matrix = Matrix()
matrix.setRotate(90F)
bitmap = Bitmap.CrateBitmap(bitmap,0,0,bitmap.width,bitmap.height, matrix, true)
pic_show.setImageBitmap(bitmap)
}
}
6. 相机水印功能
修改相机显示布局 原始代码:
<SurfaceView
android:id="@+id/my_camera_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
修改后:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<SurfaceView
android:id="@+id/my_camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="56dp"
android:layout_marginLeft="56dp"
android:layout_marginEnd="55dp"
android:layout_marginRight="55dp"
android:layout_marginBottom="92dp"
android:text="Nova 7 / 2021 - xx - xx HH:MM:SS"
android:textColor="#ea3826"
android:textSize="20sp" />
</RelativeLayout>
说明:此处仅修改了显示预览时的水印,实际拍照后保存的照片中不含有水印。
7. 显示界面添加按钮返回功能
btn_back.setOnClickListener(){
startActivity(Intent(this, MyCameraClass::class.java))
}
|