在实际的应用程序里我们可能会使用到摄像头比如我们平时使用的淘宝,评价时可以拍照上传,还有平时使用的美颜相机。我们在自己的应用程序中也可以实现调用摄像头的功能。

Android 多媒体应用:调用摄像头拍照_ide

一、调用摄像头实现拍照功能

启动摄像头其实很简单,步骤如下。

Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);

示例

1、布局

<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"
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:id="@+id/btn_start_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调用系统摄像头"

</RelativeLayout>

2、MainActivity

public class MainActivity extends Activity
private Button mbtn_start_camera;
private ImageView mimageview;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mbtn_start_camera=(Button) findViewById(R.id.btn_start_camera);
mimageview=(ImageView) findViewById(R.id.imageview);
mbtn_start_camera.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//创建Intent设置调用拍照功能
Intent intent=new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
//指定图片的输出地址
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
//这里调用startActivityForResult是为了响应以后的操作 startActivityForResult(intent, 0x23);


}
});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==0x23){
if(resultCode==RESULT_OK){
//进行压缩
zipImage(file.getAbsolutePath());
//将拍摄的照片直接显示在界面上面
mimageview.setImageURI(Uri.fromFile(file));
}
}
}
//下面的方法是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

3、最后不要忘记添加读写Sdcard的权限

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