(原创)android中使用相机的两种方式

在社交类应用或扫描二维码的场合都需要用到手机上的摄像头

在程序中启用这一硬件主要有两类方法

1.发送intent启动系统自带的摄像应用

此应用的AndroidManifest中的intent-filter如下

<intent-filter>
                <action android:name="android.media.action.VIDEO_CAPTURE" />
                <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

发送intent的代码如下:

     Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        String path ="storage/sdcard1/picture1.jpg";
        File pic =new File(path);
        Uri uri=      Uri.fromFile(pic);
        //设定拍照后照片保存位置
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri );
        startActivity(intent);

2.自己调用API,试用Camera对象来完成拍摄保存

代码如下:

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        surfaceView = (SurfaceView) findViewById(R.id.sv);
        // 获得预览画面的接收类
        SurfaceHolder holder = surfaceView.getHolder();
        // 接收类设置callback -->surface创建的时候开启camera开始预览,退出时关闭
        // 设置数据来源自渲染引擎, 不使用自己的缓冲区.
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        camera = Camera.open();

        holder.addCallback(new Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                // TODO Auto-generated method stub
                camera.stopPreview(); // 停止摄像头预览
                camera.release();
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                // TODO Auto-generated method stub
                // 摄像头的使用
                try {
                    camera.setPreviewDisplay(holder);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }// 设置预览显示
                camera.startPreview();// 开始预览

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                    int width, int height) {
                // TODO Auto-generated method stub

            }
        });
    }

    public void click(View v) {
        shoot();
    }

    public void shoot() {

        camera.takePicture(null, null, new PictureCallback() {

            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                // TODO Auto-generated method stub
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
                        data.length);

                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream("sdcard/pic1.jpg");
                    boolean isSuccess = bitmap.compress(CompressFormat.JPEG,
                            50, fos);
                    if (isSuccess) {
                        Toast.makeText(MainActivity.this, "拍摄和存储成功", 0).show();
                    } else {
                        Toast.makeText(MainActivity.this, "拍摄和存储失败", 0).show();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MEDIA_MOUNTED);

        intent.setData(Uri.fromFile(new File("/sdcard")));
        sendBroadcast(intent);

        camera.startPreview();

    }

两种方法的对比

前一种方法需要跳出本程序,而且灵活性较低,但是简单易用,代码较少便于使用和维护.

后一种方法虽然比较复杂,但是可以自己定制拍摄activity的样式以及功能选项,并且在本应用中就可以完成拍摄的功能,用处也很多.

时间: 2024-08-03 20:08:41

(原创)android中使用相机的两种方式的相关文章

转:在android中button响应的两种方式

1. 在布局文件中添加button的监听名字 Android:onClick="buttonOnClick" 例如: <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1&q

Android中activity传值的两种方式

第一种:第一个Activity  /**      * 通过这个方法跳转到activity2界面*/     public void gotoActivity2(View v){      //创建一个意图      Intent intent=new Intent(this,MainActivity2.class);                //第一种传值方式      Bundle bundle=new Bundle();      bundle.putString("name&quo

实验--使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用(杨光)

使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 攥写人:杨光  学号:20135233 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验要求: 选择一个系统调用(13号系统调用time除外),系统调用列表参见http://codelab.shiyanlou.com/xref/linux-3.18.6/arch/x86/syscalls/sys

Android模拟点击的两种方式

导论 在Android中模拟一个点击事件有两种方式是通过模拟MotionEvent来实现:一种是通过ADB来实现 第一种:模拟MotionEvent 通用方法如下: private void setSimulateClick(View view, float x, float y) { long downTime = SystemClock.uptimeMillis(); final MotionEvent downEvent = MotionEvent.obtain(downTime, dow

使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用

使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 选择调用的进程为 24 i386 getuid sys_getuid1647 i386 getgid sys_getgid16 使用库函数API方式 使用C代码中嵌入汇编代码方式

Android攻城狮 Android中更新UI的几种方式

Android中更新UI的几种方式: 1. Activity 的 runOnUiThread() 2. Handler 的 post() 3. Handler 的 sendMessage() 4. View 的 post() 1 public class FiveActivity extends Activity { 2 3 private TextView textView; 4 5 private Handler handler = new Handler() { 6 public void

java中设置代理的两种方式

1 前言 有时候我们的程序中要提供可以使用代理访问网络,代理的方式包括http.https.ftp.socks代理.比如在IE浏览器设置代理. 那我们在我们的java程序中使用代理呢,有如下两种方式.直接上代码. 2 采用设置系统属性 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import jav

JavaWeb应用中初始化Log4j的两种方式

本文主要介绍了普通JavaWeb应用(基于Tomcat)中初始化Log4j的两种方式: 1.通过增加 InitServlet ,设置令其自启动来初始化 Log4j . 2.通过监听器 ServletContextListener 监听 ServletContext 的初始化事件来初始化 Log4j . 先来看下方式一,直接上代码: web.xml 编写如下: 1 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 2 xmlns

用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用

姓名:王晨光 学号:20133232 王晨光 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 本周的实验相比较前面3次实验较为容易理解,这次实验的浅析了系统调用的工作过程,通过用库函数API和C代码嵌入汇编代码两种方式使用同一个系统调用. 我这次实验选择了20号系统调用getpid来获取进程ID.首先我先在网上查阅了getpid函数是用来获取目前进程的ID,许多程序利用取到的此值