电子大神的日记本,供应链专家的功夫茶盘,在这里记录、分享与共鸣。

登录以开始

同時兩個surfaceview camera 中的一些開發問題

問題1:

如何將camera surfaceview 顯示在最上層:用了this.setZOrderOnTop(true);這個方法

問題2:

如何用一個BUTTON來自由控制camera 可以出現預覽效果或者取消預覽效果,但是仍然能得到camera 回調數據

方法是:

        lleftLayout.removeView(cameraPreview);

        cameraPreview.setZOrderOnTop(true/false);///這個方法將當前的camera surface 帶到最上層
        lleftLayout.addView(cameraPreview);

 

問題3:

如果出現以下異常:

04-09 12:51:30.475: E/AndroidRuntime(3980): FATAL EXCEPTION: main
04-09 12:51:30.475: E/AndroidRuntime(3980): java.lang.RuntimeException: Method called after release()
04-09 12:51:30.475: E/AndroidRuntime(3980):  at android.hardware.Camera.setHasPreviewCallback(Native Method)
04-09 12:51:30.475: E/AndroidRuntime(3980):  at android.hardware.Camera.access$600(Camera.java:120)
04-09 12:51:30.475: E/AndroidRuntime(3980):  at android.hardware.Camera$EventHandler.handleMessage(Camera.java:617)

主要引起是因為在

camera.stopPreview();
  camera.release();
前沒有將setPreviewCallback 設置為null,

解決情況:

public void surfaceDestroyed(SurfaceHolder holder) {
  camera.setPreviewCallback(null) ;
  camera.stopPreview();
  camera.release();
  camera = null;
  Log.e("CameraPreview", "sufaceDestroyed---------camera release");
 }

////////////////////////////////////////////////////////////////////////////////////////////

直接上原碼:

package com.test.surfaceview;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback ,PreviewCallback{
 SurfaceHolder mHolder;
 private Camera camera;
 public CameraPreview(Context context) {
  super(context);
  mHolder = getHolder();
  mHolder.addCallback(this);
  mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 }
  public void surfaceCreated(SurfaceHolder holder) {
  camera = Camera.open();
  camera.setPreviewCallback(this);

  try {
   camera.setPreviewDisplay(holder);
  } catch (IOException exception) {
   camera.release();
   camera = null;
  }
 }

 public void surfaceDestroyed(SurfaceHolder holder) {
  camera.setPreviewCallback(null) ;
  camera.stopPreview();
  camera.release();
  camera = null;
  Log.e("CameraPreview", "sufaceDestroyed---------camera release");
 }

 public void surfaceChanged(SurfaceHolder holder, int format, int w,
   int h) {
  Camera.Parameters parameters = camera.getParameters();
  List<Camera.Size> lSize = parameters.getSupportedPreviewSizes();
  Log.i("size", "width="+lSize.get(0).width+",height="+lSize.get(0).height);
//  parameters.setPreviewSize(200, 200);
  camera.setParameters(parameters);
  camera.startPreview();
 }
 @Override
 public void onPreviewFrame(byte[] arg0, Camera arg1) {
  // TODO Auto-generated method stub
  Log.d("CameraPreview","-------onPreviewFrame------");
 }
}


package com.test.surfaceview;

 

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class OtherSurfaceView extends SurfaceView {
 Canvas canvas;
 private SurfaceHolder mHolderControl=null;
 public OtherSurfaceView(Context context) {
  super(context);
   mHolderControl=this.getHolder();
   mHolderControl.addCallback(new SurfaceHolder.Callback(){

   @Override
   public void surfaceChanged(SurfaceHolder holder, int format,
     int width, int height) {
    // TODO Auto-generated method stub
    
   }

   @Override
   public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    draw();
   }

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    
   }});
  // TODO Auto-generated constructor stub
 }

 private void draw(){
  Log.d("TAG", "---------------");
  canvas=mHolderControl.lockCanvas();
  if(mHolderControl == null || canvas == null){
   return;
  }
  ----------------------------------------------------------------------

 

package com.test.surfaceview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;

public class TestMultiSurfaceviewActivity extends Activity {
    /** Called when the activity is first created. */
 private Button btSwitchBlue=null;
 private boolean bShowCamera = true;
 private  LinearLayout lleftLayout;
 private CameraPreview cameraPreview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.main);
        FrameLayout lLayout =new FrameLayout(this);
//        LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
//       
//        lLayout.setLayoutParams(lp);
        this.addContentView(lLayout,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
       
       
        lleftLayout =new LinearLayout(this);
        LinearLayout.LayoutParams lpCamera =  new LinearLayout.LayoutParams(300,400);
      //  lpCamera.weight = 1;
        lleftLayout.setLayoutParams(lpCamera);
        lLayout.addView(lleftLayout);
       
        cameraPreview = new CameraPreview(this);
        cameraPreview.setZOrderOnTop(true);
        lleftLayout.addView(cameraPreview);
       
        lLayout.bringChildToFront(lleftLayout);
       
       
       
       
        LinearLayout lrightLayout =new LinearLayout(this);
        LinearLayout.LayoutParams lpOther =  new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
       // lpOther.weight = 1;
        lLayout.addView(lrightLayout);
       
        OtherSurfaceView otherSurfaceView = new OtherSurfaceView(this);
        otherSurfaceView.setLayoutParams(lpOther);
        lrightLayout.addView(otherSurfaceView);
       
       
        //btSwitchBlue.findViewById(R.id.show);
        btSwitchBlue = new Button(this);
        
         btSwitchBlue.setText("hide");
         FrameLayout.LayoutParams lpbutton =  new FrameLayout.LayoutParams(120,80);
         lpbutton.gravity = Gravity.BOTTOM | Gravity.RIGHT;
         btSwitchBlue.setLayoutParams(lpbutton);
         btSwitchBlue.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    if (!bShowCamera)    {  
        lleftLayout.removeViewInLayout(cameraPreview);
        cameraPreview = new CameraPreview(TestMultiSurfaceviewActivity.this);
        cameraPreview.setZOrderOnTop(true);
        /*lrightLayout.removeView(otherSurfaceView);
        cameraPreview.setZOrderOnTop(true);
        lrightLayout.addView(otherSurfaceView);*/
        bShowCamera = true;
        
        lleftLayout.addView(cameraPreview);
       // camera.startPrevie();
        //lLayout.bringChildToFront(lrightLayout);
        
        btSwitchBlue.setText("hide");  
       }
       else if(bShowCamera){
        
        lleftLayout.removeViewInLayout(cameraPreview);
        cameraPreview = new CameraPreview(TestMultiSurfaceviewActivity.this);
        cameraPreview.setZOrderOnTop(false);
        lleftLayout.addView(cameraPreview);
        bShowCamera = false;
        btSwitchBlue.setText("show");
       }
    
   }});
         //btSwitchBlue.setBackgroundColor(Color.RED);
         lLayout.addView(btSwitchBlue);
    }   
}

博主
sealove518@163.com
~学无止境~
本店主要经营: 1 TI的元件 2 Cypress的USB FIFO SRAM以及PSoc等 3 Freescale 的IC和开发板 4 Cypress各种原装USB/PSoc/仿真器和开发套件 http//soez.taobao.com QQ:120076915
点击跳转