注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

是缘是情是童真

如果你用笑脸面对 你会看见别人的笑脸

 
 
 

日志

 
 

Android调用照相功能  

2011-07-29 11:18:14|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

android 中调用照相功能有两种方法

一种是调用系统自带的camera:能够用Intent直接开启

另一种是利用SurfaceView:能够添加自己想要的功能

下面是代码:

方法一

package cn.mydreamy.zhao.photo;

import java.io.File;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.util.Log;

public class Photograph_I extends Activity {

 /** Called when the activity is first created. */

 private String logTag = "Exception";

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  // setContentView(R.layout.main);

  try {

   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

   File out = new File(Environment.getExternalStorageDirectory(),
     "camera.jpg");

   Uri uri = Uri.fromFile(out);

   intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

   startActivityForResult(intent, 0);

  } catch (Exception e) {

   e.printStackTrace();

  }

 }

 protected void onActivityResult(int requestCode, int resultCode, Intent data)

 {

  try {

   Log.v("", "requestCode = " + requestCode + " resultCode =  "
     + resultCode);

   if (requestCode != 0) {

    return;

   }

   if (resultCode == 0) {

    finish();

    return;

   }

   // Bundle extras = data.getExtras();

   // Bitmap b = (Bitmap) extras.get("data");

   Intent intent = new Intent();

   intent.setClass(this, ShowImageActivity.class);

   // intent.putExtra("image",b);

   this.startActivity(intent);

  } catch (Exception e) {

   // TODO: handle exception

   Log.v(logTag, e.getMessage());

  }

 }

}

 

另一个类

 package cn.mydreamy.zhao.photo;

import cn.mydreamy.zhao.photo.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class ShowImageActivity extends Activity {
 private String logTag = "exception";
 private AlertDialog alertDialog;
 private ImageView view;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.show);
  try {
   view = (ImageView) findViewById(R.id.view);
   // Bundle bundle = this.getIntent().getExtras();
   // Bitmap b = bundle.getParcelable("image");
   String pathString = Environment.getExternalStorageDirectory()
     .toString()
     + "/camera.jpg";
   Log.v("", "pathString = " + pathString);
   Bitmap b = BitmapFactory.decodeFile(pathString);
   view.setImageBitmap(b);
   // setContentView(view);
   alertDialog = new AlertDialog.Builder(ShowImageActivity.this)
     .setMessage("Feels Like IS感觉就像IS尽量晃动手机,体验IS的无限动感。")
     .setPositiveButton("ok",
       new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,
          int whichButton) {
         /* User clicked OK so do some stuff */
        }
       }).setNegativeButton("cancle",
       new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,
          int whichButton) {
         /* User clicked Cancel so do some stuff */
        }
       }).create();
   alertDialog.show();
  } catch (Exception e) {
   Log.v(logTag, e.getMessage());
   throw new RuntimeException(e);

  }
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  // TODO Auto-generated method stub
  Log.v("", "event = " + event.getX());
  return super.onTouchEvent(event);
 }

}
show.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <SurfaceView android:id="@+id/camera"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ImageView android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>

 

方法2:

package cn.mydreamy.zhao.photo;

import java.io.IOException;


import cn.mydreamy.zhao.photo.R;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.SurfaceHolder.Callback;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class Photograph_S extends Activity implements Callback, OnClickListener {
 /** Called when the activity is first created. */
 private SurfaceView mSurfaceView;
 private SurfaceHolder mSurfaceHolder;
 private Camera mCamera;
 private boolean mPreviewRunning;
 private ImageView mImageView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFormat(PixelFormat.TRANSLUCENT);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  setContentView(R.layout.main);
  mSurfaceView = (SurfaceView) findViewById(R.id.camera);
  mImageView = (ImageView) findViewById(R.id.image);
  mImageView.setVisibility(View.GONE);
  mSurfaceView.setOnClickListener(this);
  mSurfaceHolder = mSurfaceView.getHolder();
  mSurfaceHolder.addCallback(this);
  mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
   int height) {
  if (mPreviewRunning) {
   mCamera.stopPreview();
  }
  Parameters params = mCamera.getParameters();
  params.setPictureFormat(PixelFormat.JPEG);// 设置图片格式

  // params.setPreviewSize(width, height);
  params.set("rotation", 90);
  mCamera.setParameters(params);
  try {
   mCamera.setPreviewDisplay(holder);
  } catch (IOException e) {
   e.printStackTrace();
  }
  mCamera.startPreview();
  mPreviewRunning = true;
 }

 private AutoFocusCallback mAutoFocusCallBack = new AutoFocusCallback() {

  @Override
  public void onAutoFocus(boolean success, Camera camera) {

   Log.v("AutoFocusCallback", "AutoFocusCallback" + success);
   Camera.Parameters Parameters = mCamera.getParameters();
   Parameters.setPictureFormat(PixelFormat.JPEG);// 设置图片格式
   mCamera.setParameters(Parameters);
   mCamera.takePicture(mShutterCallback, null, mPictureCallback);
  }
 };

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  // TODO Auto-generated method stub
  mCamera = Camera.open();
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  // TODO Auto-generated method stub
  mCamera.stopPreview();
  mPreviewRunning = false;
  mCamera.release();
  mCamera = null;
 }

 /**
  * 拍照的回调接口
  */
 PictureCallback mPictureCallback = new PictureCallback() {
  @Override
  public void onPictureTaken(byte[] data, Camera camera) {
   Log.v("PictureCallback", "…onPictureTaken…");
   if (data != null) {
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
      data.length);
    mImageView.setImageBitmap(bitmap);
    mImageView.setVisibility(View.VISIBLE);
    mSurfaceView.setVisibility(View.GONE);
    if (mPreviewRunning) {
     mCamera.stopPreview();
     mPreviewRunning = false;
    }
   }
  }
 };
 /**
  * 在相机快门关闭时候的回调接口,通过这个接口来通知用户快门关闭的事件,
  * 普通相机在快门关闭的时候都会发出响声,根据需要可以在该回调接口中定义各种动作, 例如:使设备震动
  */
 ShutterCallback mShutterCallback = new ShutterCallback() {

  public void onShutter() {
   // just log ,do nothing
   Log.v("ShutterCallback", "…onShutter…");
  }

 };

 public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
   if (mCamera != null) {
    // mCamera.takePicture(null, null,mPictureCallback);
    mCamera.autoFocus(mAutoFocusCallBack);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
   }
  }
  return super.onKeyDown(keyCode, event);
 }

 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  Log.v("onClick", "…onClick…");
  mCamera.autoFocus(mAutoFocusCallBack);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  
  menu.add(0, 1, 0, "调用系统照相机");
  return true;
 }

 @Override
 public boolean onMenuItemSelected(int featureId, MenuItem item) {
  // TODO Auto-generated method stub
  switch(item.getItemId()){
  case 1:
  Intent intent = new Intent();
  intent.setClass(Photograph_S.this, Photograph_I.class);
  startActivity(intent);
  break;
  }
  return true;
 }
}
main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <SurfaceView android:id="@+id/camera"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ImageView android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>

  评论这张
 
阅读(791)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018