Android手机实现拍照的2种方法


Android系统的手机现在特别多,市场份额还在增加。

开发照相功能,实现大致有2种方法,可供大家参考:

1.调用系统摄像头来拍照

首先,找到AndroidManifest.xml文件里加入用户权限

<uses-permission android:name="android.permission.CAMERA"></uses-permission>

 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

其次,在主类java文件里加入2个控件(button和imageview),是用来触发按钮事件和显示图片的,纯是个人爱好

final int TAKE_PICTURE = 1;//为了表示返回方法中辨识你的程序打开的相机

关键是这里:startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), TAKE_PICTURE);

是打开系统自带相机,以下是处理拍照得到的数据,将数据保存下来

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TAKE_PICTURE) {
            if (resultCode == RESULT_OK) {
                Bitmap bm = (Bitmap) data.getExtras().get("data");
                img.setImageBitmap(bm);//想图像显示在ImageView视图上,private ImageView img;
                File myCaptureFile = new File("sdcard/123456.jpg");
                try {
     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
    
           bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
          
         
           bos.flush();
          
         
           bos.close();
    } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
            }
        }
}

这样就能实现调用系统自带的摄像头了,很简单的操作。

2.自己写程序来保存照片

 

照片格局文件lay.xml里要先进行这些定义

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="130px"
    android:paddingRight="200px"
  >
  <SurfaceView
    android:id="@+id/mSurfaceView1"
    android:visibility="visible"
    android:layout_width="320px"
    android:layout_height="240px">
  </SurfaceView>
  </LinearLayout>
  </LinearLayout>

 

其中SurfaceView是用来进行预览的,

在Oncreat函数里初始化一系列的值:

 requestWindowFeature(Window.FEATURE_NO_TITLE);
 setContentView(R.layout.lay);

 
     DisplayMetrics dm = new DisplayMetrics();
     getWindowManager().getDefaultDisplay().getMetrics(dm);
    // mImageView01 = (ImageView) findViewById(R.id.myImageView1);
    
   
     mSurfaceView01 = (SurfaceView) findViewById(R.id.mSurfaceView1);
    
   
     mSurfaceHolder01 = mSurfaceView01.getHolder();
    
   
     mSurfaceHolder01.addCallback(takephoto.this);
    
   
     mSurfaceHolder01.setType
     (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

首先进行初始化照相机的功能函数和参数设置:

private Camera mCamera01;

mCamera01 = Camera.open();

 
    Camera.Parameters parameters = mCamera01.getParameters();
        
  
    parameters.setPictureFormat(PixelFormat.JPEG);
    Log.i(TAG, "pic is jpeg");
        
  
    parameters.setPreviewSize(320, 240);
    Log.i(TAG, "pic pingmu fenbianlv");
        
  
    parameters.setPictureSize(1024, 768);
    Log.i(TAG, "pic tupian fenbianlv");
        
  
    mCamera01.setParameters(parameters);
        
  
    mCamera01.setPreviewDisplay(mSurfaceHolder01);
        
  
    mCamera01.startPreview();

初始化成功后就可以进行拍照了,拍照函数依然是通过调用camera类的函数来实现

mCamera01.takePicture
       (shutterCallback, rawCallback, jpegCallback);

只需实现jpegCallback这个回调函数来就行解码、保存即可,前2个参数可以直接设为null,不过系统一般会自动帮你把这些都写进来的

 private PictureCallback jpegCallback = new PictureCallback()
   {
     public void onPictureTaken(byte[] _data, Camera _camera)
     {
       // TODO Handle JPEG image data
      
     
       Bitmap bm = BitmapFactory.decodeByteArray
                   (_data, 0, _data.length);
      
     
              picname = "sdcard/1234566.jpg";//要保存在哪里,路径你自己设
       File myCaptureFile = new File(picname);
       try
       {
         BufferedOutputStream bos = new BufferedOutputStream
         (new FileOutputStream(myCaptureFile));
        
       
         bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        
       
         bos.flush();
        
       
         bos.close();
        
         
         //mImageView01.setImageBitmap(bm);
        
       
         resetCamera();
        
          }
       catch (Exception e)
       {
         Log.e(TAG, e.getMessage());
       }
     }
   };

拍照完了要重置照相机,然后可以继续拍照


   private void resetCamera()
   {
     if (mCamera01 != null && bIfPreview)
     {
       mCamera01.stopPreview();
     
       mCamera01.release();
       mCamera01 = null;
       bIfPreview = false;
     }
   }

2种拍照方式的比较

1.调用系统自带的照相机,照片格式大小只有几种选择,照片拍出来比较大,而自己程序实现的话可以调节照片大小为任意尺寸,图片的容量可以调节

2.调用系统的简单,而且外观一般比自己设置的要好看

3.调用系统的操作简单、方便,不易出错,自己编程的话需要注意,容易引起系统出错意外终止。

相关内容