Android中使用系统相机是很方便的,单这仅仅是简单的使用而已,并不能获得什么特殊的效果。
要想让应用有相机的action,咱们就必须在清单文件中做一些声明,好让系统知道,如下
action的作用就是声明action的类型,便于Intent的使用,category的作用就是注册,没有它。相关操作将不起作用。
一种方式是简单粗暴的实现,如下
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQ_1);
//然后在 onActivityResult方法中实现数据的获取,此处是展示在了一个ImageView上
if(resultCode==RESULT_OK){
if(requestCode==REQ_1){
Bundle bundle=data.getExtras();
Bitmap bitmap=(Bitmap) bundle.get("data");
imageView.setImageBitmap(bitmap);
}123456789123456789
小总结:这样的好处是简单快捷,但是在现在的android智能机中,好多相片都是很大的,这里获得的仅仅是一个缩略图罢了
另外一种方式是稍微温婉一点了,而且效果也更好一点,好处就在于它是先将照片信息存储到本地一个临时文件中,然后让ImageView去相关路径下进行读取,这样就可以获得清晰度很高的图片了。如下
/*
* 此方法的存在意义就是不在onActivityResult方法的data中获取我们拍照的缩略图,而是从我们的文件输出目录下直接查看原图
* 这样的好处就是可以对大容量的照片进行便捷的准确的操作
*/
public void onStartCarema2(View view){
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//见你给你路径传递回需要的处理方法中
Uri uri=Uri.fromFile(new File(myFilePath));
//设置文件的输出路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQ_2);
}
//然后在onActivityResult方法中进行相关的处理就可以了
else if(requestCode==REQ_2){
FileInputStream fis=null;
try {
fis=new FileInputStream(myFilePath);
Bitmap bitmap=BitmapFactory.decodeStream(fis);
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//记得最后一定要关闭相关的流操作。否则会引起相关的异常的。123456789101112131415161718192021222324252627282930313233123456789101112131415161718192021222324252627282930313233
开发自定义的相机
由于开发自定义的相机要进行相关的权限的生命,所以一定不要忘记在清单文件中做相关的处理,如下
1212
然后有以下几个步骤:
创建Camera,并完成初始化Camera,开始预览,释放资源三个方法
与Activity的SurfaceView进行绑定。
在系统的onPause(),onResume()方法中进行相关状态设置
对Camera进行参数设置,作用就是对照片类型和状态进行相关的设置
将拍得的照片进行展示,一般会新开一个Activity,用ImageView进行承载,我们还可以在此Activity上添加TextView,实现水印效果等其他的美化操作
另外,如果想加入自动聚焦的功能,就可以在SurfaceView上添加onClickListener(),对屏幕进行侦听,调用myCamera.autoFocus(null);方法即可
以上就是整个思路
接下来就是使用系统Camera的代码展示
(可以直接copy相关代码块,添加到你的应用中去,实现Camera这一功能。)
首先是MainActivity
布局
<linearlayout p="" <="" 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<button< p="">
android:id="@+id/startCarema"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:text="StartCarema"
android:onClick="onStartCarema"
/>
<button< p="">
android:id="@+id/startCarema2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:text="StartCarema2"
android:onClick="onStartCarema2"
/>
<button< p="">
android:id="@+id/customCarema"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:text="CustomCarema"
android:onClick="onCustomCarema"
/>
<imageview< p="">
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
123456789101112131415161718192021222324252627282930313233343536373839404142434445123456789101112131415161718192021222324252627282930313233343536373839404142434445
代码
package com.example.camerademo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.app.Activity;
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.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
//为下面的获取请求所用
private static int REQ_1=1;
private static int REQ_2=2;
Button btn_startCareme,btn_startCarema2,btn_customCarema;
ImageView imageView;
//定义照片存储的路径
private String myFilePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_startCareme=(Button) findViewById(R.id.startCarema);
btn_startCarema2=(Button) findViewById(R.id.startCarema2);
btn_customCarema=(Button) findViewById(R.id.customCarema);
imageView=(ImageView) findViewById(R.id.imageview);
//初始化不同手机的SD卡的路径
myFilePath=Environment.getExternalStorageDirectory().getPath();
myFilePath=myFilePath+"/"+"temperature.png";
}
public void onCustomCarema(View view){
Intent intent=new Intent(this,CustomCarema.class);
startActivity(intent);
}
public void onStartCarema(View view){
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQ_1);
}
/*
* 此方法的存在意义就是不在onActivityResult方法的data中获取我们拍照的缩略图,而是从我们的文件输出目录下直接查看原图
* 这样的好处就是可以对大容量的照片进行便捷的准确的操作
*/
public void onStartCarema2(View view){
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//见你给你路径传递回需要的处理方法中
Uri uri=Uri.fromFile(new File(myFilePath));
//设置文件的输出路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQ_2);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
if(requestCode==REQ_1){
Bundle bundle=data.getExtras();
Bitmap bitmap=(Bitmap) bundle.get("data");
imageView.setImageBitmap(bitmap);
}else if(requestCode==REQ_2){
FileInputStream fis=null;
try {
fis=new FileInputStream(myFilePath);
Bitmap bitmap=BitmapFactory.decodeStream(fis);
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
宁波好的整形医院http://www.iyestar.com