zoukankan      html  css  js  c++  java
  • 直接调用系统Camera

    关键思路:

    初始化 组件;

    创建并启动拍照intent;

    使用回调函数onActivityResult()处理图像。

    关键代码:

    初始化 组件:

    takePicBtn = (Button) findViewById(R.id.buttonPicture);
    takePicBtn.setOnClickListener(takePiClickListener);
    
    takeVideoBtn = (Button) findViewById(R.id.buttonVideo);
    takeVideoBtn.setOnClickListener(takeVideoClickListener);


    创建并启动拍照intent:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

    private final OnClickListener takePiClickListener = new View.OnClickListener()
        {
    
            @Override
            public void onClick(View v)
            {
                Log.d(LOG_TAG, "Take Picture Button Click");
                //创建新的intent—————— 利用系统自带的相机应用:拍照
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
                //创建保存图片的文件—————— create a file to save the image
                fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    
                // 此处这句intent的值设置关系到后面的onActivityResult中会进入那个分支,即关系到data是否为null,如果此处指定,则后来的data为null——180行代码
                // 设置图片文件的名称——————set the image file name
                intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    
                //启动图片捕获intent
                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }
    
        };

    使用回调函数onActivityResult()处理图像:

    // 如果是拍照
            if (CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE == requestCode)
            {
                Log.d(LOG_TAG, "CAPTURE_IMAGE");
                           
                if (RESULT_OK == resultCode)
                {
                    
                    Log.d(LOG_TAG, "RESULT_OK");
    
                    if (data != null)
                    {
                        // 没有指定特定存储路径的时候
                        Log.d(LOG_TAG,"data is NOT null, file on default position.");
    
                    }
                    
                    else
                    {
    
                        Log.d(LOG_TAG, "data IS null, file saved on target position.");
                                           
                        // Resize the full image to fit in out image view.
                       
                        // Decode the image file into a Bitmap sized to fill the View
                       
                        Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), factoryOptions);
    
                        imageView.setImageBitmap(bitmap);
                    }
                   
                }
                else if (resultCode == RESULT_CANCELED)
                {
                    // User cancelled the image capture
                }
                else
                {
                    // Image capture failed, advise user
                }
            }
  • 相关阅读:
    什么是内存(一):存储器层次结构
    关于跨平台的一些认识
    适合小白/外行的git与github最基础最浅显教程
    Android动画(二)-属性动画
    Android动画(一)-视图动画与帧动画
    View学习(四)-View的绘制(draw)过程
    View学习(三)- View的布局(layout)过程
    View学习(二)-View的测量(measure)过程
    View学习(一)-DecorView,measureSpec与LayoutParams
    wcf的诡异问题
  • 原文地址:https://www.cnblogs.com/mmcmmc/p/3945581.html
Copyright © 2011-2022 走看看