博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android开发学习之路--Camera之初体验
阅读量:6957 次
发布时间:2019-06-27

本文共 9143 字,大约阅读时间需要 30 分钟。

    顾名思义Camera就是拍照和录像的功能,像微信里面,我们想拍照传一下照片,就能够通过camera来拍照,然后存储照片。发送给好友。那么微信的app里面是不会直接通过camera api来实现的,由于系统一般都会有camera这个程序,那么直接调用camera app来实现拍照的功能不是非常方便嘛。这里我们学习下。事实上终于camera调用到android底层的是v4l2的接口,关于v4l2,还有android的camera的框架以后有机会再好好研究研究。

    调用系统自带的camera须要用到intent,通过MediaStore获取照片路径。以下来试一下。新建projectCameraPictureTest。为layout加入代码例如以下:

> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="10dp"> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照"/> </LinearLayout>

    编写代码例如以下:

package com.example.jared.camerapicturetest;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.ImageView;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;public class MainActivity extends AppCompatActivity {    public static final int TAKE_PHOTO = 1;    public static final int CROP_PICTURE = 2;    private Button takePhoto;    private ImageView picture;    private Uri imageUri;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        takePhoto = (Button)findViewById(R.id.take_photo);        takePhoto.setOnClickListener(new myOnClickListener());        picture = (ImageView)findViewById(R.id.picture);        picture.setOnClickListener(new myOnClickListener());    }    private class myOnClickListener implements View.OnClickListener {        @Override        public void onClick(View view) {            switch (view.getId()) {                case R.id.take_photo:                    setTakePhoto();                    break;                default:                    break;            }        }    }    public void setTakePhoto() {        File outputImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");        try {            if(outputImage.exists()) {                outputImage.delete();            }            outputImage.createNewFile();        } catch (IOException e) {            e.printStackTrace();        }        imageUri = Uri.fromFile(outputImage);        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);        startActivityForResult(intent, TAKE_PHOTO);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode) {            case TAKE_PHOTO:                if(resultCode == RESULT_OK) {                    Intent intent1 = new Intent("com.android.camera.action.CROP");                    intent1.setDataAndType(imageUri, "image/*");                    intent1.putExtra("scale", true);                    intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);                    startActivityForResult(intent1, CROP_PICTURE);                }                break;            case CROP_PICTURE:                if(resultCode == RESULT_OK) {                    try {                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()                                .openInputStream(imageUri));                        picture.setImageBitmap(bitmap);                    } catch (FileNotFoundException e) {                        e.printStackTrace();                    }                }                break;            default:                break;        }    }}
    这里首先确定了保存的路径为根文件夹下的test.jpg,然后通过intent,传入这个路径的Uri,打开相机进行拍照,这里有对拍照的返回,假设返回成功,那么就调用CROP的功能对比片进行裁剪,进入到裁减后返回成功就把图片显示在layout创建的ImageView中。

    详细须要真机显示,这里再插播一段关于真机屏幕在mac电脑上的显示,详细能够參考这篇文章。。通过一个chrome的Vysor插件来实现,须要android的5.0以上的版本号才干够。

    好了,以下看下显示的效果:

     

    效果基本上出来了。非常不错的插件。

微信里面非常多不是直接拍照发送的,还有通过选择相冊的图片,已经拍好的照片来发送图片的,那么接着我们来实现这个功能。首先layout加入了choosephoto:

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" android:layout_margin="10dp"> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照"/> <Button android:id="@+id/choose_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选取照片"/> </LinearLayout>

    接着改动MainActivity代码例如以下:

package com.example.jared.camerapicturetest;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.ImageView;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;public class MainActivity extends AppCompatActivity {    public static final int TAKE_PHOTO = 1;    public static final int CROP_PICTURE = 2;    private Button takePhoto;    private Button choosePhoto;    private ImageView picture;    private Uri imageUri;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        takePhoto = (Button)findViewById(R.id.take_photo);        takePhoto.setOnClickListener(new myOnClickListener());        choosePhoto = (Button)findViewById(R.id.choose_photo);        choosePhoto.setOnClickListener(new myOnClickListener());        picture = (ImageView)findViewById(R.id.picture);        picture.setOnClickListener(new myOnClickListener());    }    private class myOnClickListener implements View.OnClickListener {        @Override        public void onClick(View view) {            switch (view.getId()) {                case R.id.take_photo:                    setTakePhoto();                    break;                case R.id.choose_photo:                    setChoosePhoto();                default:                    break;            }        }    }    public void setChoosePhoto() {        File outputImage1 = new File(Environment.getExternalStorageDirectory(), "test1.jpg");        try {            if(outputImage1.exists()) {                outputImage1.delete();            }            outputImage1.createNewFile();        } catch (IOException e) {            e.printStackTrace();        }        imageUri = Uri.fromFile(outputImage1);        Intent intent1 = new Intent("android.intent.action.GET_CONTENT");        intent1.setType("image/*");        intent1.putExtra("crop", true);        intent1.putExtra("scale", true);        intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);        startActivityForResult(intent1, CROP_PICTURE);    }    public void setTakePhoto() {        File outputImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");        try {            if(outputImage.exists()) {                outputImage.delete();            }            outputImage.createNewFile();        } catch (IOException e) {            e.printStackTrace();        }        imageUri = Uri.fromFile(outputImage);        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);        startActivityForResult(intent, TAKE_PHOTO);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode) {            case TAKE_PHOTO:                if(resultCode == RESULT_OK) {                    Intent intent1 = new Intent("com.android.camera.action.CROP");                    intent1.setDataAndType(imageUri, "image/*");                    intent1.putExtra("scale", true);                    intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);                    startActivityForResult(intent1, CROP_PICTURE);                }                break;            case CROP_PICTURE:                if(resultCode == RESULT_OK) {                    try {                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()                                .openInputStream(imageUri));                        picture.setImageBitmap(bitmap);                    } catch (FileNotFoundException e) {                        e.printStackTrace();                    }                }                break;            default:                break;        }    }}
    基本上和拍照也几乎相同。然后我们执行下看看效果:

    

     点击选择照片button。我们会进入到相冊的app里面,然后选择一张照片,然后裁剪后保存。如上图所看到的。

附:參考《第一行代码》

你可能感兴趣的文章
VMware SDS 之四:VSAN的技术细节
查看>>
Eager thick vs Lazy thick disk performance
查看>>
aix 主机信息的查看
查看>>
日志框架_Index
查看>>
java安全沙箱(一)之ClassLoader双亲委派机制
查看>>
我的友情链接
查看>>
关于HashMap详解。
查看>>
多线程环境下慎用静态变量
查看>>
话里话外:抓住核心,为推倒“部门墙” 寻找突破口
查看>>
删除此电脑下各种影视库
查看>>
python几个内置函数
查看>>
百度今天更新很大
查看>>
我的友情链接
查看>>
有线网络高可用项目实施方案(更新中)
查看>>
dpkg的用法
查看>>
notepad++开发中常用的插件
查看>>
手动发送HTTP请求调用Web Service
查看>>
巧用Office365中的Exchange Online Protection(一)
查看>>
linux入门第一讲
查看>>
我的友情链接
查看>>