您的位置:首页 > 其它

手机拍照功能

2015-11-24 14:33 169 查看
xml

<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:orientation="vertical"
>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="takePhoto"
android:id="@+id/take_photo"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="choicePhoto"
android:id="@+id/choice_photo"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="600dp"
android:id="@+id/my_photo"/>

</LinearLayout>


MainActivity.java

package com.example.testsometing;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.transform.OutputKeys;

import junit.framework.Protectable;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends BaseActivity {
Button btn_takePhoto;
Button btn_choicephone;
ImageView img_photo;
Uri imageUri;
File outputFile;
static final int TAKE_PHOTO = 1;
static final int CROP_PHOTO = 2;
static final int CHOICE_PHONE = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_takePhoto = (Button) this.findViewById(R.id.take_photo);
img_photo = (ImageView) this.findViewById(R.id.my_photo);
btn_takePhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
outputFile = new File("/storage/emulated/0/DCIM/Camera","imgxPhoto.jpg");
if(outputFile.exists()){
outputFile.delete();
}else{
//不做处理
}
try {
outputFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputFile);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
//将所拍图片的路径设置为imagerUri,如果不做设置则为相机拍照的默认路径
//设置路径后“照片”是看不到所拍的图片,要进入“文件”才能看到所拍的照片
startActivityForResult(intent, TAKE_PHOTO);
}
});

btn_choicephone = (Button) this.findViewById(R.id.choice_photo);
btn_choicephone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
File file = new File("/storage/emulated/0/DCIM/Camera","myphotoy.jpg");
if(file.exists()){
file.delete();
}
else{
//不做处理
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(file);
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
//不设置Type会报错
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
/*              intent.putExtra("scale", true);//规模,要不要都一样,暂时看不出区别
*/              intent.putExtra("crop",true);//可以剪切
startActivityForResult(intent, CHOICE_PHONE);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO:
if(resultCode == RESULT_OK){
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri,"image");
/*intent.putExtra("scale",true);*/
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent, CROP_PHOTO);
}
break;
case CROP_PHOTO:
if(resultCode == RESULT_OK){
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
//如果对所拍的照片不做剪切处理则bitmap不能得到目标,因为所拍图片太大
img_photo.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
case CHOICE_PHONE:
if(resultCode == RESULT_OK){
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
//如果对所拍的照片不做剪切处理则bitmap不能得到目标,因为所拍图片太大
img_photo.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}

}


效果

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