android自定义相机(带边框和按钮)

本文介绍了如何在Android应用中创建自定义相机,包括设置蓝色边框背景和添加操作按钮。通过使用SurfaceView来实现相机功能,并在拍照后对图片进行压缩,以适应服务器上传要求。文章提供了关键代码示例,包括相机初始化、图片保存路径设置及拍照后的处理步骤。
摘要由CSDN通过智能技术生成

前两个月项目要求不能调用系统的相机,那就只能用自定义的了,查了一些资料,自己再研究了一下,自定义的相机还是有点复杂的,布局和代码中都要用到一个重要的SurfaceView

一、建立布局,布局的背景框可以让美工给出,这里姑且就是一个蓝色的边框,然后下面有三个按钮,我里布局文件activity_custom_camera.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <FrameLayout
            android:id="@+id/layout_camera"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible">

            <SurfaceView
                android:id="@+id/surfaceView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="visible"/>
            
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/view_text_top"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:text="请将证件对准方框"
                    android:textSize="18dp"
                    android:visibility="visible"
                    android:textColor="@color/blue"
                    android:background="#000000"
                    android:alpha="0.5"/>

                <LinearLayout
                    android:id="@+id/view_main_content"
                    android:layout_marginTop="50dp"
                    android:layout_above="@+id/layout_button_btn"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <View
                        android:id="@+id/view_left"
                        android:layout_width="30dp"
                        android:layout_height="match_parent"
                        android:background="#000000"
                        android:alpha="0.5"
                        android:visibility="visible"/>
                    
                    <!--中心布局,取景处-->
                    <View
                        android:id="@+id/bg_center_view"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:background="@drawable/qr_code_bg_take_photo"/>

                    <TextView
                        android:id="@+id/view_right"
                        android:textColor="@color/blue"
                        android:gravity="center"
                        android:layout_width="30dp"
                        android:layou
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值