zoukankan      html  css  js  c++  java
  • 使用Intent 启动相机拍照

    在开发中我们可能需要使用手机拍照,为了减少工作量,我们可以直接使用Intent来启动系统自带的照相机。

    使用Intent打开相机。可以指定照片的输出位置及文件名。然后在启动的Acitivity中onActivityResult获取返回的图像。

    注意:如果给照相机指定了照片的输出位置。那么将接收不到返回的图像内容

    activity_main.xml

    <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" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回预览图像" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/button1"
            android:text="存储到本地" />
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="320dp"
            android:layout_height="240dp"
            android:layout_below="@id/button1" />
    
    </RelativeLayout>
    View Code

    Acitvity.cs

    public class MainActivity extends Activity {
    
        Button btnTakePicture;
        Button btnTakePicture2;
        ImageView imageView1;
    
        int takeCode = 1;
        Uri outPutFile;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btnTakePicture = (Button) findViewById(R.id.button1);
            btnTakePicture2 = (Button) findViewById(R.id.button2);
            imageView1 = (ImageView) findViewById(R.id.imageView1);
    
            btnTakePicture.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    // 直接启动照相机,照相机照片将会存在默认的文件中
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, takeCode);
    
                }
            });
    
            // 指定
            btnTakePicture2.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    // 指定图片存储地址
                    File file = new File(Environment.getExternalStorageDirectory(),
                            "test.jpg");
                    outPutFile = Uri.fromFile(file);
                    // 启动相机
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutFile);
                    startActivityForResult(intent, takeCode);
    
                }
            });
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (requestCode == takeCode) {
    
                if (data != null) {
                    // 如果不指定图片保存地址,那么将会返回照片的缩略图,返回的照片大小是被压缩过的。
                    if (data.hasExtra("data")) {
                        Bitmap bitmap = data.getParcelableExtra("data");
                        imageView1.setImageBitmap(bitmap);
                    }
                } else {
    
                    // 如果给照相机指定了图片存储位置,那么返回的data是null的,需要从图片路径加载图片。生成缩略图,使用这种办法可以防止返回的缩略图尺寸太小的关系
                    int width = imageView1.getWidth();
                    int height = imageView1.getHeight();
    
                    BitmapFactory.Options opts = new BitmapFactory.Options();
    
                    int imageWidth = opts.outWidth;
                    int imageHeight = opts.outHeight;
    
                    // 图像缩小比例
                    int scale = Math.min(imageWidth / width, imageHeight / height);
    
                    // 把图像大小设置成imageview 大小
                    opts.inJustDecodeBounds = false;
                    opts.inSampleSize = scale;
                    opts.inPurgeable = true;
    
                    Bitmap bitmap = BitmapFactory.decodeFile(outPutFile.getPath(),
                            opts);
    
                    imageView1.setImageBitmap(bitmap);
                }
            }
    
        }
    
    }
    View Code

    为了保证有些手机能正确使用照相机,需要在AndroidManifest.xml 中添加照相机权限。

    <users-permission android:name="android.permission.CAMERA"/>

    PS:有兴趣编写一个自己的照相机app的话可以考虑一下camera类

  • 相关阅读:
    20165231 2017-2018-2 《Java程序设计》第5周学习总结
    结对学习感想及创意照
    20165231 第四周测试课下补做
    20165231 2017-2018-2 《Java程序设计》第4周学习总结
    20165231 2017-2018-2 《Java程序设计》第3周学习总结
    20165309 第七周学习总结
    20165309 实验一 Java开发环境的熟悉
    20165309 第四周测验总结
    20165309 第四周学习总结
    20165317第七周学习内容
  • 原文地址:https://www.cnblogs.com/ywtk/p/3867266.html
Copyright © 2011-2022 走看看