Camera 拍照

1、背景

同样这篇文章是为了练习代码的抽取能力,其实Camera的拍照后我们会获取到原始照片的数据,这些数据我们可以进行处理;比如加个滤镜,美白等等;这就是我们的美白相机了,但对于图片的处理,我们最好在底层处理;然后把处理的图片数据再转换成图片,最后展示给用户,美白相机的大体流程就是这样的,不过今天主要练习代码的抽取能力;虽然上次已经写过了,只有经过反复的训练,我们才能对怎样抽取代码,并进行封装得心用手;

2、方法(来自疯狂Androud讲义)

Android应用提供了Camera来控制拍照,使用Camera进行拍照比较简单,按如下步骤进行即可。
1:调用Camera的open()方法打开相机。该方法默认打开后置摄像头。如果需要打开指定摄像头,可以为该方法传入声响头ID.
2:调用Camera的getParameters()方法获取拍照参数,该方法返回一个Camera.Parameters对象。
3:调用Camera.Parameters对象方法设置拍照参数;
4:调用Camera的startPreview()方法开始预览取景,在预览取景之前调用Camera的setPreviewDisplay(SurfaceViewHolder holder)方法设置使用哪个SurfaceView来显示取景图片。
5:调用Camera的takePicture()方法进行拍照。
6:结束程序时,调用Camera的stopPreview()结束取景预览,并调用release()方法释放资源;

3、实例

该实例通过预览,当我们点击拍照后,会弹出一个对话框,让用户输入图片的名字;

1、MainActivity布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <SurfaceView 
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:layout_marginBottom="8dp"
        >
        <Button 
            android:id="@+id/btn_capture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/normal_capture"
            />
    </LinearLayout>
</RelativeLayout>

2、对话框布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp"
        >
        <TextView 
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="文件名:"
            android:textSize="20sp"
            />
        <EditText 
            android:id="@+id/et_filename"
            android:layout_width="match_parent"
            androi
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值