您的位置:首页 > 移动开发 > Android开发

android 调用系统相机和相册

2016-02-18 14:12 537 查看
Android应用中,很多时候我们需要实现上传图片,或者直接调用手机上的拍照功能拍照处理然后直接显示并上传功能,下面将讲述调用相机拍照处理图片然后显示和调用手机相册中的图片处理然后显示的功能,要想实现上传功能,一般都是上传到数据库中,将imageView中的图片取出来然后存到数据库中即可。

下面讲述实现的步骤:

1. 调用相册中的图片裁剪然后显示。

1.1 使用Intent获取从相册中选择的照片。

1.2 对获取的图片进行裁剪处理,裁剪处理也是使用Intent调用的Android自带的裁剪功能实现。

1.3 将处理过的数据显示在imageView中。

2.调用拍照功能获得图片裁剪然后显示。

2.1 使用Intent获取拍照获得的照片。

2.2 对获取的图片进行裁剪处理,裁剪处理也是使用Intent调用的Android自带的裁剪功能实现。

2.3 将处理过的数据显示在imageView中。

注意:上面的两种实现方法其实主要是要学会如何使用Intent调用Android自带的功能和手机中的各种文件。

下面是实现的代码:

1. 布局文件

view
sourceprint?

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


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


03.
android:orientation=
"vertical"


04.
android:layout_width=
"fill_parent"


05.
android:layout_height=
"fill_parent"
>


06.
<ImageView
 android:id=
"@+id/imageView"


07.
android:adjustViewBounds=
"true"


08.
android:layout_gravity=
"center"


09.
android:minWidth=
"150dip"


10.
android:minHeight=
"150dip"


11.
android:layout_width=
"wrap_content"


12.
android:layout_height=
"wrap_content"
/>


13.
<Button
 android:id=
"@+id/btnPhone"


14.
android:layout_width=
"fill_parent"


15.
android:layout_height=
"wrap_content"


16.
android:text=
"相册"
/>


17.
<Button
 android:id=
"@+id/btnTakePicture"


18.
android:layout_height=
"wrap_content"


19.
android:layout_width=
"fill_parent"


20.
android:text=
"拍照"
/>


21.
</LinearLayout>


2.activity文件

view
sourceprint?

001.
import
java.io.ByteArrayOutputStream;


002.
import
java.io.File;


003.


004.
import
android.app.Activity;


005.
import
android.content.Intent;


006.
import
android.graphics.Bitmap;


007.
import
android.net.Uri;


008.
import
android.os.Bundle;


009.
import
android.os.Environment;


010.
import
android.provider.MediaStore;


011.
import
android.view.View;


012.
import
android.widget.Button;


013.
import
android.widget.ImageView;


014.


015.
public
class
MainActivity
extends
Activity
 {


016.


017.
private
static
final
int
NONE
 =
0
;


018.
private
static
final
int
PHOTO_GRAPH
 =
1
;
//
 拍照


019.
private
static
final
int
PHOTO_ZOOM
 =
2
;
//
 缩放


020.
private
static
final
int
PHOTO_RESOULT
 =
3
;
//
 结果


021.
private
static
final
String
 IMAGE_UNSPECIFIED =
"image/*"
;


022.
private
ImageView
 imageView =
null
;


023.
private
Button
 btnPhone =
null
;


024.
private
Button
 btnTakePicture =
null
;


025.


026.
@Override


027.
public
void
onCreate(Bundle
 savedInstanceState) {


028.
super
.onCreate(savedInstanceState);


029.
setContentView(R.layout.activity_main);


030.


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


032.
btnPhone
 = (Button) findViewById(R.id.btnPhone);


033.
btnPhone.setOnClickListener(onClickListener);


034.
btnTakePicture
 = (Button) findViewById(R.id.btnTakePicture);


035.
btnTakePicture.setOnClickListener(onClickListener);


036.
}


037.


038.
private
final
View.OnClickListener
 onClickListener =
new
View.OnClickListener()
 {


039.


040.
@Override


041.
public
void
onClick(View
 v) {


042.
if
(v==btnPhone){
//从相册获取图片


043.
Intent
 intent =
new
Intent(Intent.ACTION_PICK,
null
);


044.
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
 IMAGE_UNSPECIFIED);


045.
startActivityForResult(intent,
 PHOTO_ZOOM);


046.
}
else
if
(v==btnTakePicture){
//从拍照获取图片


047.
Intent
 intent =
new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);


048.
intent.putExtra(MediaStore.EXTRA_OUTPUT,
 Uri.fromFile(
new
File(Environment


049.
.getExternalStorageDirectory(),
"temp.jpg"
)));


050.
startActivityForResult(intent,
 PHOTO_GRAPH);


051.
}


052.


053.
}


054.


055.
};


056.


057.
@Override


058.
protected
void
onActivityResult(
int
requestCode,
int
resultCode,
 Intent data) {


059.
if
(resultCode
 == NONE)


060.
return
;


061.
//
 拍照


062.
if
(requestCode
 == PHOTO_GRAPH) {


063.
//
 设置文件保存路径


064.
File
 picture =
new
File(Environment.getExternalStorageDirectory()


065.
+
"/temp.jpg"
);


066.
startPhotoZoom(Uri.fromFile(picture));


067.
}


068.


069.
if
(data
 ==
null
)


070.
return
;


071.


072.
//
 读取相册缩放图片


073.
if
(requestCode
 == PHOTO_ZOOM) {


074.
startPhotoZoom(data.getData());


075.
}


076.
//
 处理结果


077.
if
(requestCode
 == PHOTO_RESOULT) {


078.
Bundle
 extras = data.getExtras();


079.
if
(extras
 !=
null
)
 {


080.
Bitmap
 photo = extras.getParcelable(
"data"
);


081.
ByteArrayOutputStream
 stream =
new
ByteArrayOutputStream();


082.
photo.compress(Bitmap.CompressFormat.JPEG,
75
,
 stream);
//
 (0-100)压缩文件


083.
//此处可以把Bitmap保存到sd卡中,具体请看:http://www.cnblogs.com/linjiqin/archive/2011/12/28/2304940.html


084.
imageView.setImageBitmap(photo);
//把图片显示在ImageView控件上


085.
}


086.


087.
}


088.


089.
super
.onActivityResult(requestCode,
 resultCode,data);


090.
}


091.


092.
/**


093.
*
 收缩图片


094.
*


095.
*
@param
uri


096.
*/


097.
public
void
startPhotoZoom(Uri
 uri) {


098.
Intent
 intent =
new
Intent(
"com.android.camera.action.CROP"
);
//调用Android系统自带的一个图片剪裁页面,


099.
intent.setDataAndType(uri,
 IMAGE_UNSPECIFIED);


100.
intent.putExtra(
"crop"
,
"true"
);
//进行修剪


101.
//
 aspectX aspectY 是宽高的比例


102.
intent.putExtra(
"aspectX"
,
1
);


103.
intent.putExtra(
"aspectY"
,
1
);


104.
//
 outputX outputY 是裁剪图片宽高


105.
intent.putExtra(
"outputX"
,
300
);


106.
intent.putExtra(
"outputY"
,
500
);


107.
intent.putExtra(
"return-data"
,
true
);


108.
startActivityForResult(intent,
 PHOTO_RESOULT);


109.
}


110.


111.
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: