当前位置: 代码迷 >> Android >> Android调用Camera实现自定义拍照
  详细解决方案

Android调用Camera实现自定义拍照

热度:103   发布时间:2016-04-28 08:13:59.0
Android调用Camera实现自定义照相

首先是布局文件,线性布局,左边两个按钮垂直摆放,右边预览图以及照相获取到的图:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="horizontal" > 
  6.  
  7.     <LinearLayout 
  8.         android:layout_width="200dip" 
  9.         android:layout_height="150dip" 
  10.         android:orientation="vertical" > 
  11.  
  12.         <Button 
  13.             android:id="@+id/btn1" 
  14.             android:layout_width="80dip" 
  15.             android:layout_height="50dip" 
  16.             android:text="Start takPic" /> 
  17.  
  18.         <Button 
  19.             android:id="@+id/btn2" 
  20.             android:layout_width="80dip" 
  21.             android:layout_height="50dip" 
  22.             android:text="Get Pic" /> 
  23.     </LinearLayout> 
  24.  
  25.     <LinearLayout 
  26.         android:id="@+id/cameraView" 
  27.         android:layout_width="200dip" 
  28.         android:layout_height="150dip" 
  29.         android:orientation="vertical" > 
  30.     </LinearLayout> 
  31.  
  32. </LinearLayout> 

代码,都写在一起的。包含一个自定义的surfaceView(用于相机的照相预览).以及activit:

  1. package com.nico; 
  2.  
  3. import java.io.ByteArrayInputStream; 
  4. import java.io.IOException; 
  5.  
  6. import android.app.Activity; 
  7. import android.content.Context; 
  8. import android.graphics.PixelFormat; 
  9. import android.graphics.drawable.BitmapDrawable; 
  10. import android.graphics.drawable.Drawable; 
  11. import android.hardware.Camera; 
  12. import android.os.Bundle; 
  13. import android.os.Environment; 
  14. import android.util.Log; 
  15. import android.view.SurfaceHolder; 
  16. import android.view.SurfaceView; 
  17. import android.view.View; 
  18. import android.view.Window; 
  19. import android.view.WindowManager; 
  20. import android.view.View.OnClickListener; 
  21. import android.widget.Button; 
  22. import android.widget.LinearLayout; 
  23.  
  24. public class Test extends Activity implements OnClickListener { 
  25.     /** Called when the activity is first created. */ 
  26.  
  27.     // camera 类 
  28.     private Camera camera = null
  29.     // 继承surfaceView的自定义view 用于存放照相的图片 
  30.     private CameraView cv = null
  31.  
  32.     // 回调用的picture,实现里边的onPictureTaken方法,其中byte[]数组即为照相后获取到的图片信息 
  33.     private Camera.PictureCallback picture = new Camera.PictureCallback() { 
  34.  
  35.         @Override 
  36.         public void onPictureTaken(byte[] data, Camera camera) { 
  37.             // 主要就是将图片转化成drawable,设置为固定区域的背景(展示图片),当然也可以直接在布局文件里放一个surfaceView供使用。 
  38.             ByteArrayInputStream bais = new ByteArrayInputStream(data); 
  39.             Drawable d = BitmapDrawable.createFromStream(bais, Environment 
  40.                     .getExternalStorageDirectory().getAbsolutePath() 
  41.                     + "/img.jpeg"); 
  42.             l.setBackgroundDrawable(d); 
  43.             try { 
  44.             } catch (Exception e) { 
  45.                 e.printStackTrace(); 
  46.             } 
  47.         } 
  48.  
  49.     }; 
  50.  
  51.     // 按钮 布局等定义,不作赘述 
  52.     Button btn1 = null
  53.     Button btn2 = null
  54.     LinearLayout l = null
  55.  
  56.     @Override 
  57.     public void onCreate(Bundle savedInstanceState) { 
  58.         super.onCreate(savedInstanceState); 
  59.  
  60.         requestWindowFeature(Window.FEATURE_NO_TITLE); 
  61.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
  62.                 WindowManager.LayoutParams.FLAG_FULLSCREEN); 
  63.         getWindow().setFormat(PixelFormat.TRANSLUCENT); 
  64.         setContentView(R.layout.main); 
  65.  
  66.         l = (LinearLayout) findViewById(R.id.cameraView); 
  67.         btn1 = (Button) findViewById(R.id.btn1); 
  68.         btn2 = (Button) findViewById(R.id.btn2); 
  69.  
  70.         btn1.setOnClickListener(this); 
  71.         btn2.setOnClickListener(this); 
  72.     } 
  73.  
  74.     //主要的surfaceView,负责展示预览图片,camera的开关 
  75.     class CameraView extends SurfaceView { 
  76.  
  77.         // 
  78.         private SurfaceHolder holder = null
  79.  
  80.         public CameraView(Context context) { 
  81.             super(context); 
  82.             holder = this.getHolder(); 
  83.  
  84.             holder.addCallback(new SurfaceHolder.Callback() { 
  85.  
  86.                 @Override 
  87.                 public void surfaceChanged(SurfaceHolder holder, int format, 
  88.                         int width, int height) { 
  89.                     Camera.Parameters parameters = camera.getParameters(); 
  90.                     //以下注释掉的是设置预览时的图像以及拍照的一些参数 
  91.                     // parameters.setPictureFormat(PixelFormat.JPEG); 
  92.                     // parameters.setPreviewSize(parameters.getPictureSize().width, 
  93.                     // parameters.getPictureSize().height); 
  94.                     // parameters.setFocusMode("auto"); 
  95.                     // parameters.setPictureSize(width, height); 
  96.                     camera.setParameters(parameters); 
  97.                     camera.startPreview(); 
  98.                 } 
  99.  
  100.                 @Override 
  101.                 public void surfaceCreated(SurfaceHolder holder) { 
  102.                     camera = Camera.open(); 
  103.  
  104.                     try { 
  105.                         //设置camera预览的角度,因为默认图片是倾斜90度的 
  106.                         camera.setDisplayOrientation(90); 
  107.                         //设置holder主要是用于surfaceView的图片的实时预览,以及获取图片等功能,可以理解为控制camera的操作.. 
  108.                         camera.setPreviewDisplay(holder); 
  109.                     } catch (IOException e) { 
  110.                         camera.release(); 
  111.                         camera = null
  112.                         e.printStackTrace(); 
  113.                     } 
  114.  
  115.                 } 
  116.  
  117.                 @Override 
  118.                 public void surfaceDestroyed(SurfaceHolder holder) { 
  119.                     //顾名思义可以看懂 
  120.                     camera.stopPreview(); 
  121.                     camera.release(); 
  122.                     camera = null
  123.                 } 
  124.             }); 
  125. //          holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
  126.         } 
  127.     } 
  128.  
  129.     @Override 
  130.     public void onClick(View v) { 
  131.         if (v == btn1) { 
  132.             l.removeAllViews(); 
  133.             cv = new CameraView(Test.this); 
  134.             LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( 
  135.                     LinearLayout.LayoutParams.FILL_PARENT, 
  136.                     LinearLayout.LayoutParams.FILL_PARENT); 
  137.             l.addView(cv, params); 
  138.         } else if (v == btn2) { 
  139.             camera.takePicture(nullnull, picture); 
  140.         } 
  141.     } 
  142. }
  相关解决方案