调用摄像头拍照,拿到缩略图,本地图,裁剪

通过两个按钮,分别拍照获取图片,第一个拿到的是缩略图,第二个是自己保存的图,直接上代码吧:

布局,

<?xml version="1.0" encoding="utf-8"?>
<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"
   >


    <Button
        android:id="@+id/start_camera_thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="takePhotoThumbnail"
        android:text="拍照缩略图" />
    <Button
        android:id="@+id/start_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="takePhotoLocal"
        android:layout_below="@id/start_camera_thumbnail"
        android:text="拍照本地图" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"

        android:src="@mipmap/ic_launcher"
        />
</RelativeLayout>


Java 代码:

package com.example.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {




    private ImageView imageView ;
    private Uri imageUri ;
    private  File outputIamge = null ;
    private static final int TAKE_PHOTO_THUMBNAIL = 0 ;
    private static final int TAKE_PHOTO_LOCAL = 1 ;
    private static final int TAKE_CROP = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView)findViewById(R.id.image);

    }
    //拿到缩略图
    public void takePhotoThumbnail(View view)
    {
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

        startActivityForResult(intent,TAKE_PHOTO_THUMBNAIL);
    }
    //本地图
    public void takePhotoLocal(View view)
    {
        outputIamge = new File(Environment.getExternalStorageDirectory(),"photo.jpg");
        try
        {
            if(outputIamge.exists())
            {
                outputIamge.delete();
            }

            outputIamge.createNewFile();

        }catch (IOException e)
        {
            e.printStackTrace();
        }

        imageUri = Uri.fromFile(outputIamge);
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
        startActivityForResult(intent,TAKE_PHOTO_LOCAL);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       switch (requestCode) {

           case TAKE_PHOTO_THUMBNAIL:
               Bundle bundle = data.getExtras();
               Bitmap bitmap = (Bitmap)bundle.get("data");
               imageView.setImageBitmap(bitmap);
               break;
           case TAKE_PHOTO_LOCAL:
               startPhotoZoom(Uri.fromFile(outputIamge));
               break;

           case TAKE_CROP:
               Bitmap decodeFile = BitmapFactory.decodeFile(outputIamge.getAbsolutePath());
               imageView.setImageBitmap(decodeFile);
       }

    }



    //裁剪
    public void startPhotoZoom(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 400);
        intent.putExtra("outputY", 400);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        startActivityForResult(intent, TAKE_CROP);
    }

}

 

1:拿缩略图,通过Bundle bundle = data.getExtras(); Bitmap bitMap = (Bitmap)bundle.get("data");就能拿到缩略图,一般这个不能满足需求。所以还是要拿原图。

2:通过 new File(Environment.getExternalStorageDirectory(),“photo.jpg”");,创建目录。最终拿到本地图片,并且展示(上面代码有)

最后,注册权限:

拍照权限:  <uses-permission android:name="android.permission.CAMERA"/>
 写到外部存储的权限    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

,ok了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值