package com.cloay.camera;
|
002 |
003 |
import java.io.FileNotFoundException;
|
004 |
005 |
import android.app.Activity;
|
006 |
import android.app.AlertDialog;
|
007 |
import android.content.ContentResolver;
|
008 |
import android.content.Intent;
|
009 |
import android.graphics.Bitmap;
|
010 |
import android.graphics.BitmapFactory;
|
011 |
import android.net.Uri;
|
012 |
import android.os.Bundle;
|
013 |
import android.view.MotionEvent;
|
014 |
import android.view.View;
|
015 |
import android.view.View.OnClickListener;
|
016 |
import android.view.View.OnTouchListener;
|
017 |
import android.widget.ImageButton;
|
018 |
import android.widget.ImageView;
|
019 |
/** |
020 |
* 打开照相机或者图库添加图片
|
021 |
* CameraTestActivity.java
|
022 |
* @author Cloay
|
023 |
* 2011-11-30
|
024 |
*/
|
025 |
public class CameraTestActivity extends Activity {
|
026 |
private ImageButton camera;
|
027 |
private ImageView image;
|
028 |
@Override
|
029 |
public void onCreate(Bundle savedInstanceState) {
|
030 |
super .onCreate(savedInstanceState);
|
031 |
setContentView(R.layout.main);
|
032 |
|
033 |
image = (ImageView) findViewById(R.id.image);
|
034 |
camera = (ImageButton) findViewById(R.id.camera);
|
035 |
camera.setOnTouchListener( new OnTouchListener() {
|
036 |
|
037 |
@Override
|
038 |
public boolean onTouch(View v, MotionEvent event) {
|
039 |
if (event.getAction() == MotionEvent.ACTION_DOWN){
|
040 |
v.setBackgroundResource(R.drawable.camera_bg_pressed);
|
041 |
}
|
042 |
if (event.getAction() == MotionEvent.ACTION_UP){
|
043 |
showMenuDialog();
|
044 |
v.setBackgroundResource(R.drawable.camera_bg_normal);
|
045 |
}
|
046 |
return false ;
|
047 |
}
|
048 |
});
|
049 |
}
|
050 |
|
051 |
private void showMenuDialog() {
|
052 |
View menuView = View.inflate(CameraTestActivity. this , R.layout.menu, null );
|
053 |
final AlertDialog menuDialog = new AlertDialog.Builder(CameraTestActivity. this )
|
054 |
.setView(menuView)
|
055 |
.setTitle( "选择操作" )
|
056 |
// .setIcon(R.drawable.camera) |
057 |
.create();
|
058 |
menuDialog.show();
|
059 |
menuView.findViewById(R.id.camera).setOnClickListener( new OnClickListener() {
|
060 |
|
061 |
@Override
|
062 |
public void onClick(View v) {
|
063 |
menuDialog.dismiss();
|
064 |
Intent intentCamera = new Intent( "android.media.action.IMAGE_CAPTURE" ); //使用照相机
|
065 |
startActivityForResult(intentCamera, Activity.DEFAULT_KEYS_DIALER);
|
066 |
}
|
067 |
});
|
068 |
menuView.findViewById(R.id.picture).setOnClickListener( new OnClickListener() {
|
069 |
|
070 |
@Override
|
071 |
public void onClick(View v) {
|
072 |
menuDialog.dismiss();
|
073 |
// Intent intentPhoto = new Intent(Intent.ACTION_GET_CONTENT, null); |
074 |
// intentPhoto.setType("image/*"); //这个参数是确定要选择的内容为图片 |
075 |
// intentPhoto.putExtra("crop", "circle"); //这个参数 不太懂,唯一知道的是:设置了参数,就会调用裁剪,如果不设置,就会跳过裁剪的过程。 |
076 |
// intentPhoto.putExtra("aspectX", 33); //这个是裁剪时候的 裁剪框的 X 方向的比例。 |
077 |
// intentPhoto.putExtra("aspectY",43); //同上Y 方向的比例. (注意: aspectX, aspectY ,两个值都需要为 整数,如果有一个为浮点数,就会导致比例失效。) |
078 |
//设置aspectX 与 aspectY 后,裁剪框会按照所指定的比例出现,放大缩小都不会更改。如果不指定,那么 裁剪框就可以随意调整了。
|
079 |
// intentPhoto.putExtra("outputX", 50); //返回数据的时候的 X 像素大小。 |
080 |
// intentPhoto.putExtra("outputY", 100); //返回的时候 Y 的像素大小。 |
081 |
//以上两个值,设置之后会按照两个值生成一个Bitmap, 两个值就是这个bitmap的横向和纵向的像素值,如果裁剪的图像和这个像素值不符合,那么空白部分以黑色填充。
|
082 |
083 |
// intentPhoto.putExtra("noFaceDetection", true); // 是否去除面部检测, 如果你需要特定的比例去裁剪图片,那么这个一定要去掉,因为它会破坏掉特定的比例。 |
084 |
// |
085 |
// intentPhoto.putExtra("return-data", true); //是否要返回值。 一般都要。 |
086 |
// startActivityForResult(intentPhoto, 1); |
087 |
|
088 |
Intent intent = new Intent();
|
089 |
/* 开启Pictures画面Type设定为image */
|
090 |
intent.setType( "image/*" );
|
091 |
/* 使用Intent.ACTION_GET_CONTENT这个Action */
|
092 |
intent.setAction(Intent.ACTION_GET_CONTENT);
|
093 |
/* 取得相片后返回本画面 */
|
094 |
startActivityForResult(intent, 2 );
|
095 |
}
|
096 |
});
|
097 |
}
|
098 |
099 |
@Override
|
100 |
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
|
101 |
if (requestCode == 2 ){
|
102 |
if (resultCode == RESULT_OK) {
|
103 |
Uri uri = data.getData();
|
104 |
ContentResolver cr = this .getContentResolver();
|
105 |
try {
|
106 |
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
|
107 |
/* 将Bitmap设定到ImageView */
|
108 |
image.setImageBitmap(bitmap);
|
109 |
} catch (FileNotFoundException e) {
|
110 |
image.setImageResource(R.drawable.empty);
|
111 |
}
|
112 |
}
|
113 |
}
|
114 |
if (requestCode == Activity.DEFAULT_KEYS_DIALER){
|
115 |
if (data != null ){
|
116 |
Bundle extras = data.getExtras();
|
117 |
Bitmap bitmap = (Bitmap) extras.get( "data" );
|
118 |
image.setImageBitmap(bitmap);
|
119 |
}
|
120 |
else {
|
121 |
image.setImageResource(R.drawable.empty);
|
122 |
}
|
123 |
}
|
124 |
super .onActivityResult(requestCode, resultCode, data);
|
125 |
}
|
126 |
|
127 |
|
128 |
} |
相关推荐
Android4.0.3系统照相机源码。
android照相机应用源码,可以直接运行
自定义Android照相机,实现只拍摄矩形区域.完美解决预览及拍照时的照片拉伸失真,旋转等问题
Android 录像照相机录像录音
delphi xe6 android 调用照相机摄像头拍照片,简单非常实用的原代码,无特定组件,真机调试没问题.
android 照相机 demo android 照相机 demo android 照相机 demo android 照相机 demo android 照相机 demo android 照相机 demo
android 调用照相机拍照,其中对对焦进行了一定的优化
delphi xe5 android 调用照相机摄像头拍照片camera,简单非常实用的原代码,无特定组件!
Android应用源码之基于Android2.2照相机Demo,可以直接放到项目里使用.zip
Android自定义照相机实现.docx
1.android获取本地图片; 2.android系统照相机,拍照保存,获取图片。
android4.0照相机源码camera源码
android4.0 照相机源代码 camera 照相机源代码
Android应用源码之基于Android2.2照相机Demo,可以直接放到项目里使用.zip项目安卓应用源码下载Android应用源码之基于Android2.2照相机Demo,可以直接放到项目里使用.zip项目安卓应用源码下载 1.适合学生毕业设计研究...
android调用照相机,非常好用,可以实现调用手机自带德相机进行操作
delphi xe5 android 调用照相机获取拍的照片
android 照相机 自动对焦