Android拍照

Android中的拍照常用的有两种:

  • 拍照获取缩略图
    private static final int ACTION_TAKE_PHOTO_S = 2;
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(takePictureIntent, ACTION_TAKE_PHOTO_S);

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==ACTION_TAKE_PHOTO_S&&resultCode==RESULT_OK){
            Bundle extras = data.getExtras();
            mImageBitmap = (Bitmap) extras.get("data");
            mImageView.setImageBitmap(mImageBitmap);

        }
    }
  • 拍照获取原图
private static final int ACTION_TAKE_PHOTO_B = 1;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//TODO f是拍照后照片保存的文件
        File f = null;
//TODO 保存文件名,拍照完成后获取图片要用到
        mCurrentPhotoPath = f.getAbsolutePath();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(f));
        startActivityForResult(takePictureIntent, ACTION_TAKE_PHOTO_B );

        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==ACTION_TAKE_PHOTO_S&&resultCode==RESULT_OK){
            //TODO 到这里拍摄的图片已经保存到指定的文件中了
        }
        }

对于图片的缩放,可以参考
http://blog.csdn.net/guchuanhang/article/details/43835585

  • 对拍照逻辑的封装
package com.example.android.photobyintent;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.ECLAIR)
public class CameraUtil {
    private static final String JPEG_FILE_PREFIX = "IMG_";
    private static final String JPEG_FILE_SUFFIX = ".jpg";
    private static final String CAMERA_DIR = "/dcim/";

    /**该方法需要在onActivityResult进行处理
     * @return filePath
     */
    public static String takePictureIntent(Context packageContext,
            int requestCode) {
        String imgFilePath = null;
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(packageContext
                .getPackageManager()) == null) {
            Toast.makeText(packageContext, "抱歉您没有相机APP,无法拍照~",
                    Toast.LENGTH_LONG).show();
        } else {
            File f = null;
            try {
                f = setUpPhotoFile(packageContext);
                imgFilePath = f.getAbsolutePath();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(f));
            } catch (IOException e) {
                e.printStackTrace();
                f = null;
            }
            ((Activity) packageContext).startActivityForResult(
                    takePictureIntent, requestCode);
        }
        return imgFilePath;
    }

    @TargetApi(Build.VERSION_CODES.FROYO)
    private static File setUpPhotoFile(Context context) throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA)
                .format(new Date());
        String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
        File albumF = null;

        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {

            String albumName = context.getString(R.string.app_name);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
                albumF = new File(
                        Environment
                                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                        albumName);
            } else {
                albumF = new File(Environment.getExternalStorageDirectory()
                        + CAMERA_DIR + albumName);
            }
            if (albumF != null) {
                if (!albumF.mkdirs()) {
                    if (!albumF.exists()) {
                        Log.d("CameraSample", "failed to create directory");
                        return null;
                    }
                }
            }

        } else {
            Log.v(context.getString(R.string.app_name),
                    "External storage is not mounted READ/WRITE.");
        }

        File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX,
                albumF);
        return imageF;
    }

    /**
    将图片加入图库
     */
    public static void img2Galley(Context context, String imgPath) {
        Intent mediaScanIntent = new Intent(
                "android.intent.action.MEDIA_SCANNER_SCAN_FILE");
        File f = new File(imgPath);
        if (f != null) {
            Uri contentUri = Uri.fromFile(f);
            mediaScanIntent.setData(contentUri);
            context.sendBroadcast(mediaScanIntent);
        }
    }

    @TargetApi(Build.VERSION_CODES.ECLAIR)
    public static Bitmap getThumbnail(String filePath) {
        Bitmap thumbBitmap = null;

        ExifInterface exif;
        try {
            exif = new ExifInterface(filePath);
            byte[] imageData = exif.getThumbnail();
            thumbBitmap = BitmapFactory.decodeByteArray(imageData, 0,
                    imageData.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return thumbBitmap;
    }

}

封装的这个工具类中,takePictureIntent调用相机拍照,保存原图并返回图片路径。需要在页面的onActivityResult调用 CameraUtil.img2Galley(this, filePath);将图片添加到图库中。
最后需要在Manifest中添加如下参数。

<uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

本文主要参考地址:
http://developer.android.com/training/camera/photobasics.html#TaskScalePhoto
Demo下载地址:
http://download.csdn.net/detail/guchuanhang/9500418

Android拍照可以通过调用相机API来实现。以下是一个简单的示例代码,演示了如何在Android应用中实现拍照功能: ```java // 在Activity中定义一个常量,用于标识拍照请求 private static final int REQUEST_IMAGE_CAPTURE = 1; // 创建一个用于保存拍照后的图片的文件 private File photoFile; // 启动相机应用 private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 确保有相机应用可以处理拍照请求 if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // 创建一个用于保存拍照后的图片的文件 try { photoFile = createImageFile(); } catch (IOException ex) { // 处理文件创建异常 } // 如果文件创建成功,则将文件的URI传递给相机应用 if (photoFile != null) { Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } } // 创建一个用于保存拍照后的图片的文件 private File createImageFile() throws IOException { // 创建一个唯一的文件名 String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* 前缀 */ ".jpg", /* 后缀 */ storageDir /* 存储目录 */ ); return image; } // 处理拍照后的结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { // 拍照成功,可以在这里处理拍照后的图片 // 例如,将图片显示在ImageView中 ImageView imageView = findViewById(R.id.imageView); imageView.setImageURI(Uri.fromFile(photoFile)); } } ``` 请注意,上述代码中的`com.example.android.fileprovider`是一个示例文件提供器的授权名称,你需要根据你的应用包名和文件提供器的配置进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值