展会信息港展会大全

Android[高级教程] Android程序调用摄像头
来源:互联网   发布日期:2015-09-29 10:16:20   浏览:1932次  

导读:很多开发者都想在程序用来调用摄像头,并对拍出的照片进行处理。首先先对程序的进行一下预览阀先先对主页面进行设计,这里很简单,只是加了个按钮和一张图片[html] <?xml ver...

很多开发者都想在程序用来调用摄像头,并对拍出的照片进行处理。首先先对程序的进行一下预览

首先先对主页面进行设计,这里很简单,只是加了个按钮和一张图片

[html] <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<Button

android:id="@+id/button1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/camera" />

<ImageView

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<Button

android:id="@+id/button1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/camera" />

<ImageView

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher" />

</LinearLayout>

接下来就是对按钮事件进行处理,按下按钮的时候会调用本机摄像头

[java] //图片存入地址www.2cto.com

imageFilePath = Environment.getExternalStorageDirectory()

.getAbsolutePath() + "/mypicture.jpg";

File imageFile = new File(imageFilePath);

Uri imageFileUri = Uri.fromFile(imageFile);

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);

startActivityForResult(i, CAMERA_RESULT);

//图片存入地址

imageFilePath = Environment.getExternalStorageDirectory()

.getAbsolutePath() + "/mypicture.jpg";

File imageFile = new File(imageFilePath);

Uri imageFileUri = Uri.fromFile(imageFile);

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);

startActivityForResult(i, CAMERA_RESULT);

Intent直接调用了本机的拍照程序,并把拍的图片存在内存卡里,接下来就是要在我们自己的程序里显示拍的照片了

这里我们直接用了Activity里的onActivityResult这个方法,这个方法是对Activity返回结果的处理

[java] @Override

protected void onActivityResult(int requestCode, int resultCode,

Intent intent) {

// TODO Auto-generated method stub

super.onActivityResult(requestCode, resultCode, intent);

//如果拍照成功

if (resultCode == RESULT_OK) {

// Bundle extras = intent.getExtras();

// Bitmap bmp = (Bitmap)extras.get("data");

imv = (ImageView) findViewById(R.id.imageView1);

//取得屏幕的显示大小

Display currentDisplay = getWindowManager().getDefaultDisplay();

int dw = currentDisplay.getWidth();

int dh = currentDisplay.getHeight();

//对拍出的照片进行缩放

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();

bmpFactoryOptions.inJustDecodeBounds = true;

Bitmap bmp = BitmapFactory.decodeFile(imageFilePath,

bmpFactoryOptions);

int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight

/ (float) dh);

int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth

/ (float) dw);

if (heightRatio > 1 && widthRatio > 1) {

if (heightRatio > widthRatio) {

bmpFactoryOptions.inSampleSize = heightRatio;

} else {

bmpFactoryOptions.inSampleSize = widthRatio;

}

}

bmpFactoryOptions.inJustDecodeBounds = false;

bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);

imv.setImageBitmap(bmp);

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode,

Intent intent) {

// TODO Auto-generated method stub

super.onActivityResult(requestCode, resultCode, intent);

//如果拍照成功

if (resultCode == RESULT_OK) {

// Bundle extras = intent.getExtras();

// Bitmap bmp = (Bitmap)extras.get("data");

imv = (ImageView) findViewById(R.id.imageView1);

//取得屏幕的显示大小

Display currentDisplay = getWindowManager().getDefaultDisplay();

int dw = currentDisplay.getWidth();

int dh = currentDisplay.getHeight();

//对拍出的照片进行缩放

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();

bmpFactoryOptions.inJustDecodeBounds = true;

Bitmap bmp = BitmapFactory.decodeFile(imageFilePath,

bmpFactoryOptions);

int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight

/ (float) dh);

int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth

/ (float) dw);

if (heightRatio > 1 && widthRatio > 1) {

if (heightRatio > widthRatio) {

bmpFactoryOptions.inSampleSize = heightRatio;

} else {

bmpFactoryOptions.inSampleSize = widthRatio;

}

}

bmpFactoryOptions.inJustDecodeBounds = false;

bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);

imv.setImageBitmap(bmp);

}

}

这里我们就完成了对本机摄像头的调用,其实在这里主要要学习的是startActivityForResult和onActivityResult这两个方法,一个是调用Activity并将结果返回给调用的Activity,一个就是处理返回的数据了,好了,如果哪位想要程序的话,可以直接留邮件地址。

摘自 kangkangz4的专栏

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港