关于android调用相机及拍照的存储和照片处理功能的全面小案例
来源:互联网 发布:软件开发创业 编辑:程序博客网 时间:2024/04/29 12:33
java层:
package com.friendone.inteye;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CameraActivity extends Activity implements SurfaceHolder.Callback{
/* 创建私有Camera对象 */
private Camera mCamera01;
private Button mButton01, mButton02, mButton03;
/* 作为review照下来的相片之用 */
private ImageView mImageView01;
private TextView mTextView01;
private String TAG = "HIPPO";
private SurfaceView mSurfaceView01;
private SurfaceHolder mSurfaceHolder01;
String latLongString;
/* 默认相机预览模式为false */
private boolean bIfPreview = false;
Location location;
/* 将照下来的图片存储在此 */
//private String strCaptureFilePath = "/sdcard/camera_snap.jpg";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 使应用程序全屏幕运行,不使用title bar */
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.camera);
/* 判断存储卡是否存在 */
if(!checkSDCard()){
/* 提醒User未安装存储卡 */
mMakeTextToast(
getResources().getText(R.string.str_err_nosd).toString(),
true
);
}
/* 取得屏幕解析像素 */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
mTextView01 = (TextView) findViewById(R.id.myTextView1);
mImageView01 = (ImageView) findViewById(R.id.myImageView1);
/* 以SurfaceView作为相机Preview之用 */
mSurfaceView01 = (SurfaceView) findViewById(R.id.mSurfaceView1);
/* 绑定SurfaceView,取得SurfaceHolder对象 */
mSurfaceHolder01 = mSurfaceView01.getHolder();
/* Activity必须实现SurfaceHolder.Callback */
mSurfaceHolder01.addCallback(CameraActivity.this);
/*
* 以SURFACE_TYPE_PUSH_BUFFERS(3)
* 作为SurfaceHolder显示类型
* */
mSurfaceHolder01.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mButton01 = (Button)findViewById(R.id.myButton1);
mButton02 = (Button)findViewById(R.id.myButton2);
mButton03 = (Button)findViewById(R.id.myButton3);
/* 打开相机及Preview */
mButton01.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0){
/* 自定义初始化打开相机函数 */
initCamera();
}
});
/* 停止Preview及相机 */
mButton02.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0){
/* 自定义重置相机,并关闭相机预览函数 */
resetCamera();
}
});
Bundle extras=getIntent().getExtras();
latLongString=extras.getString("latLongString");
/* 拍照 */
mButton03.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0){
/* 当存储卡存在才允许拍照,存储暂存图像文件 */
if(checkSDCard()){
/* 自定义拍照函数 */
takePicture();
}
else {
/* 存储卡不存在显示提示 */
mTextView01.setText(
getResources().getText(R.string.str_err_nosd).toString()
);
}
}
});
}
//图片名称自增
/* 自定义初始相机函数 */
private void initCamera(){
if(!bIfPreview){
/* 若相机非在预览模式,则打开相机 */
try{
mCamera01 = Camera.open();
Log.i(TAG,"Camera.open()------------1");
}catch(Exception e){
Log.i("Camera.open()-------->", "Exceptione :" + e);
}
}
if (mCamera01 != null && !bIfPreview){
Log.i(TAG, "inside the camera");
/* 创建Camera.Parameters对象 */
Camera.Parameters parameters = mCamera01.getParameters();
if (Integer.parseInt(Build.VERSION.SDK) >= 8)
setDisplayOrientation(mCamera01, 90);
else {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
parameters.set("orientation", "portrait");
parameters.set("rotation", 90);
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "landscape");
parameters.set("rotation", 90);
}
}
/* 设置相片格式为JPEG */
parameters.setPictureFormat(PixelFormat.JPEG);
/* 指定preview的屏幕大小 */
parameters.setPreviewSize(320, 240);
/* 设置图片分辨率大小 */
parameters.setPictureSize(320, 240);
/* 将Camera.Parameters设置予Camera */
try{
mCamera01.setParameters(parameters);
}catch(Exception e){
Log.i("archermind-------->", "Exceptione :" + e);
}
/* setPreviewDisplay唯一的参数为SurfaceHolder */
try{
mCamera01.setPreviewDisplay(mSurfaceHolder01);
} catch (IOException e){
e.printStackTrace();
}
/* 立即运行Preview */
mCamera01.startPreview();
bIfPreview = true;
}
}
/* 拍照撷取图像 */
private void takePicture(){
if (mCamera01 != null && bIfPreview) {
/* 调用takePicture()方法拍照 */
mCamera01.takePicture(shutterCallback, rawCallback, jpegCallback);
}
}
/* 相机重置 */
private void resetCamera(){
if (mCamera01 != null && bIfPreview){
mCamera01.stopPreview();
/* 扩展学习,释放Camera对象 */
bIfPreview = false;
mCamera01.release();
mCamera01 = null;
}
}
private ShutterCallback shutterCallback = new ShutterCallback(){
public void onShutter() {
}
};
private PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] _data, Camera _camera) {
}
};
private PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] _data, Camera _camera){
/* onPictureTaken传入的第一个参数即为相片的byte */
String name = new DateFormat().format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA)) + ".jpg";
Bitmap bm = BitmapFactory.decodeByteArray(_data, 0, _data.length);
bm=generatorContactCountIcon(bm);
/* 创建新文件 */
File myCaptureFile = new File("/sdcard/"+name);
try{
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
/* 采用压缩转档方法 */
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
/* 调用flush()方法,更新BufferStream */
bos.flush();
/* 结束OutputStream */
bos.close();
/* 将拍照下来且存储完毕的图文件,显示出来 */
mImageView01.setImageBitmap(bm);
/* 显示完图文件,立即重置相机,并关闭预览 */
resetCamera();
/* 再重新启动相机继续预览 */
initCamera();
}
catch (Exception e){
Log.e(TAG, e.getMessage());
}
}
};
/* 自定义删除文件函数 */
private void delFile(String strFileName){
try{
File myFile = new File(strFileName);
if(myFile.exists()){
myFile.delete();
}
}
catch (Exception e){
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
public void mMakeTextToast(String str, boolean isLong){
if(isLong==true){
Toast.makeText(CameraActivity.this, str, Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(CameraActivity.this, str, Toast.LENGTH_SHORT).show();
}
}
private boolean checkSDCard(){
/* 判断存储卡是否存在 */
if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
return true;
}
else{
return false;
}
}
public void surfaceChanged(SurfaceHolder surfaceholder, int format, int w, int h){
Log.i(TAG, "Surface surfaceChanged");
}
public void surfaceCreated(SurfaceHolder surfaceholder){
Log.i(TAG, "Surface surfaceCreated");
}
public void surfaceDestroyed(SurfaceHolder surfaceholder){
/* 当Surface不存在,需要删除图片 */
Log.i(TAG, "Surface surfaceDestroyed");
try{
//delFile(strCaptureFilePath);
resetCamera();
}
catch(Exception e){
e.printStackTrace();
}
Log.i(TAG, "Surface Destroyed");
}
//旋转90度
protected void setDisplayOrientation(Camera camera, int angle) {
Method downPolymorphic;
try {
downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(camera, new Object[] { angle });
} catch (Exception e1) {
}
}
private Bitmap generatorContactCountIcon(Bitmap icon){
//初始化画布
int iconSize=(int)getResources().getDimension(android.R.dimen.app_icon_size);
Log.i(TAG, "the icon size is "+iconSize);
Bitmap contactIcon=Bitmap.createBitmap(320, 240, Config.ARGB_8888);
Canvas canvas=new Canvas(contactIcon);
//拷贝图片
Paint iconPaint=new Paint();
iconPaint.setDither(true);//防抖动
iconPaint.setFilterBitmap(true);//用来对Bitmap进行滤波处理,这样,当你选择Drawable时,会有抗锯齿的效果
Rect src=new Rect(0, 0, icon.getWidth(), icon.getHeight());
Rect dst=new Rect(0, 0, 320, 240);
canvas.drawBitmap(icon, src, dst, iconPaint);
//在图片上创建经纬度
String contacyCount="经纬度:"+latLongString;
//启用抗锯齿和使用设备的文本字距
Paint countPaint=new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG);
countPaint.setColor(Color.RED);
countPaint.setTextSize(20f);
countPaint.setTypeface(Typeface.DEFAULT_BOLD);
canvas.drawText(contacyCount, iconSize-60, 25, countPaint);
Log.i(TAG, "结束==========="+latLongString);
return contactIcon;
}
}
xml层:
<?xml version="1.0" encoding="utf-8"?>
< LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/blue"
android:text="@string/hello"
/>
<SurfaceView
android:id="@+id/mSurfaceView1"
android:visibility="visible"
android:layout_width="320dp"
android:layout_height="240dp">
</SurfaceView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_button1"/>
<Button
android:id="@+id/myButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_button2"/>
<Button
android:id="@+id/myButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_take_picture"/>
</LinearLayout>
<ImageView
android:id="@+id/myImageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
< /LinearLayout>
其中Intent传过来的值是获取经纬度的值。具体没有写出。
- 关于android调用相机及拍照的存储和照片处理功能的全面小案例
- 关于android调用相机及拍照的存储和照片处理功能的全面小案例
- android调用相机及拍照的存储和照片处理功能
- 调用相机拍照后返回照片过小的问题
- Android菜鸟笔记-调用相机拍照后返回照片过小的问题
- android 自定义用相机拍照后的照片存储位置
- android 调用系统相机进行拍照及照片的裁切
- 关于调用Android相机拍摄照片返回数据过小的问题
- android 调用系统相机拍照的各种异常处理
- Android调用相机拍照获取原始照片
- 关于Android开发调用系统相机拍照的 一些事
- Android调用自身相机拍照并保存拍摄的照片到手机的sd卡
- Android调用自身相机拍照并保存拍摄的照片到手机的sd卡
- 调用手机相机拍照并获取拍摄的照片
- android中调用系统的相册或者相机拍照并上传照片
- Android开发教程--关于系统相机拍照获取的照片尺寸较小问题的解决
- Android 7.0调用相机拍照,返回后显示拍照照片
- android解决相机拍照照片被旋转的问题
- zabbix为啥持续报警
- IT技术学习视频收藏...
- 从HandlerThread源码理清handler、looper与messageQueue之间的关系
- Java三大特征
- skip-grant-tables:非常有用的mysql启动参数
- 关于android调用相机及拍照的存储和照片处理功能的全面小案例
- OperatorCalc_Stack
- 基于RFS(robot framework selenium)框架模拟POST/GET请求执行自动化接口测试
- 第七届蓝桥杯省赛A组
- java远程调试配置
- Android7.0调用系统相机或相册时报错android.os.FileUriExposedException
- [jzoj]1345. 物流运输(图上DP)
- Cordova hybrid app -- android -- plugins
- OPENGL_SHARDER 实现美颜功能