多媒体之——摄像头

摄像头的模拟,在模拟器上并不能看到想要的效果,所以原来使用模拟器的同学,现在最好用真机喽^^。

用法:

 这里我们介绍的是如何调用系统的摄像头来进行拍照,大体的方式是利用Intent来启动系统的摄像头,intent中传入相应的action,这样就可启动系统的摄像头,但为了可以在拍完照后可以继续使用照片,可以用startActivityForResult的方式来启动。

这里利用一个示例来进行讲解:

范例:

功能:用摄像头拍照: 点击按钮,调用相机,拍一张照片,确定后能够存储到手机的相应位置,并显示到界面的imageview中。
使用Intent来调摄像头

public class Camera extends Activity {
    private Button mButtonCamera;
    private ImageView mImageviewCamera;
    private File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        mButtonCamera= (Button) findViewById(R.id.buttonCamera);
        mImageviewCamera= (ImageView) findViewById(R.id.imageCamera);
        mButtonCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//点击按钮,打开摄像头
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置隐式启动系统摄像机
                /*
                创建file将拍摄的照片存储
                 */
                //设置创建的文件路径,和名称(名称是当前时间,保证不会冲突)
                file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
                if(!file.exists()){//判断是否存在文件
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//告诉系统相机将照片保存的位置
                startActivityForResult(intent,0x34);//利用有返回信息的方式启动intent,需要传入一个请求码


            }
        });
    }
//当用startActivityForResult开启的intent关闭时调用此方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0x34&&resultCode==RESULT_OK){//将图片显示到imageview控件上
            mImageviewCamera.setImageURI(Uri.fromFile(file));//将file全局

        }
    }
}

权限:启动摄像头,并将拍摄的图片存到手机需要write权限。

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


详细介绍:

1.这里利用Intent中的action来隐式的启动摄像机。
Action为MediaStore.ACTION_IMAGE_CAPTURE

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

2.创建file文件,用来保存拍摄的照片

file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
                if(!file.exists()){//判断是否存在文件
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

3.告诉系统将照片保存的位置。
第一个参数固定:表输出位置;
第二个参数:通过file来获得文件的Uri。

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

4.开启摄像头。
由于我们需要返回信息,这里利用startActivityForResult来开启,当然如果不需要返回值的操作也可以利用startActivity方法。
这里的第二个参数:表示intent的请求码。在返回函数的操作中用于作为intent的标识。

startActivityForResult(intent,0x34);

5.在摄像头关闭后,会调用onActivityResult方法,在这里我们判断请求码和返回结果是否正常,然后将照片显示到Imageview中。
利用setImageURI的方法,传入file的Uri。

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0x34&&resultCode==RESULT_OK){//将图片显示到imageview控件上
            mImageviewCamera.setImageURI(Uri.fromFile(file));//将file全局

        }
    }

示例功能延伸:

功能:将拍摄的照片进行裁剪。
思路:摄像头关闭后,通过在onActivityResult方法中判断请求码,如果是仅拍摄完并未裁剪,就启动裁剪程序。否则,直接将图片显示到imageview中。

 if (resultCode == RESULT_OK) {
                    Intent intent = new Intent("com.android.camera.action.CROP");
                    intent.setDataAndType(Uri.fromFile(file), "image/*");
                    intent.putExtra("scale", true);//scale固定的key值
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                    startActivityForResult(intent, 0x30);// 启动裁剪程序

                }

可能出现的问题:

由于手机不同,可能有的拍摄的图片并不能显示,需要在设置到imageview之前进行一步压缩操作,这里可以利用google提供的压缩图片的方法:

private void zipImage(String savePath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(savePath, options);
        options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960);
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(savePath, options);
        try {
            FileOutputStream fos = new FileOutputStream(savePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        bitmap.recycle();
        bitmap = null;
        System.gc();
    }
    public int computeSampleSize(BitmapFactory.Options options,
                                 int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength,
                maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

    private int computeInitialSampleSize(BitmapFactory.Options options,
                                         int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
                .sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
                Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值