1

我需要使用没有预览的相机并拍摄多张照片。

我已经编写了我的代码,我可以拍一张,它可以正常工作。

但我不能处理不止一次。因为相机服务的逻辑我尝试了一个方法CreateCameraTakeShot,仍然没有成功。

你能帮我解决这个问题吗?

这是我的代码;

public class ContinuingCallActivity extends Activity implements SurfaceHolder.Callback{

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
    FileOutputStream outStream;


  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_continuingcall);

      //getWindow().setFormat(PixelFormat.UNKNOWN);
      surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
      surfaceHolder = surfaceView.getHolder();
      surfaceHolder.addCallback(this);
      surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


      //    for(int i=0;i<10;i++)
        //{
            mHandler.postDelayed(mLaunchTask, 1000);
        //}
}

  Handler mHandler = new Handler();


    private Runnable mLaunchTask = new Runnable() {
        @Override
        public void run() {


            camera.takePicture(null,myPictureCallback_RAW, myPictureCallback_JPG);
            //createCameraTakeShot();


        }
     };


  ShutterCallback myShutterCallback = new ShutterCallback(){

 @Override
 public void onShutter() {
  // TODO Auto-generated method stub

 }};

PictureCallback myPictureCallback_RAW = new PictureCallback(){

 @Override
 public void onPictureTaken(byte[] arg0, Camera arg1) {
  // TODO Auto-generated method stub

 }};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

 @Override
 public void onPictureTaken(byte[] arg0, Camera arg1) {
  // TODO Auto-generated method stub
  Bitmap bitmapPicture
   = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);

  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/MySight/"; 
  File newdir = new File(dir); 
  newdir.mkdirs();

Date d = new Date();
  CharSequence dateOfShot  = DateFormat.format("dd-MM-yy hh:mm:ss", d.getTime());

  String file = dir+dateOfShot+".jpg";
  File newfile = new File(file);

try {
        newfile.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

 try {
     outStream = new FileOutputStream(newfile);
     outStream.write(bytes.toByteArray());
     outStream.close();
     Log.d("Log", "onPictureTaken - wrote bytes: " + arg0.length);
 } catch (FileNotFoundException e) {
     e.printStackTrace();
 } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

 }};


public void createCameraTakeShot()
{
camera = Camera.open();

if (camera != null)
{
      try {
       camera.setPreviewDisplay(surfaceHolder);
       camera.startPreview();
       previewing = true;
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
}

camera.takePicture(null,myPictureCallback_RAW, myPictureCallback_JPG);

camera.stopPreview();


}

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

if(previewing){
  camera.stopPreview();
  previewing = false;
 }

 if (camera != null){
  try {
   camera.setPreviewDisplay(surfaceHolder);
   camera.startPreview();
   previewing = true;
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

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

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

camera.stopPreview();
 //camera.release();
 //camera = null;
 previewing = false;
}
} 
4

1 回答 1

1

您可以使用处理程序的计时器任务来快速拍照

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
       surfaceHolder = surfaceView.getHolder();
       surfaceHolder.addCallback(this);
       surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        MyTimerTask myTask = new MyTimerTask();
        Timer myTimer = new Timer();

        myTimer.schedule(myTask, 1000, 1000);        

}

class MyTimerTask extends TimerTask {
      public void run() {
          // ERROR
        camera.takePicture(null,myPictureCallback_RAW, myPictureCallback_JPG);
      }
    }



ShutterCallback myShutterCallback = new ShutterCallback(){

 @Override
 public void onShutter() {
  // TODO Auto-generated method stub

 }};

PictureCallback myPictureCallback_RAW = new PictureCallback(){

 @Override
 public void onPictureTaken(byte[] arg0, Camera arg1) {
  // TODO Auto-generated method stub

 }};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

 @Override
 public void onPictureTaken(byte[] arg0, Camera arg1) {
  // TODO Auto-generated method stub
  Bitmap bitmapPicture
   = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);

  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/MySight/"; 
  File newdir = new File(dir); 
  newdir.mkdirs();

Date d = new Date();
  CharSequence dateOfShot  = DateFormat.format("dd-MM-yy hh:mm:ss", d.getTime());

  String file = dir+dateOfShot+".jpg";
  File newfile = new File(file);

try {
        newfile.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

 try {
     outStream = new FileOutputStream(newfile);
     outStream.write(bytes.toByteArray());
     outStream.close();
     Log.d("Log", "onPictureTaken - wrote bytes: " + arg0.length);
 } catch (FileNotFoundException e) {
     e.printStackTrace();
 } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

 }};


public void createCameraTakeShot()
{
camera = Camera.open();

if (camera != null)
{
      try {
       camera.setPreviewDisplay(surfaceHolder);
       camera.startPreview();
       previewing = true;
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
}

camera.takePicture(null,myPictureCallback_RAW, myPictureCallback_JPG);


}

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

if(previewing){
  camera.stopPreview();
  previewing = false;
 }

 if (camera != null){
  try {
   camera.setPreviewDisplay(surfaceHolder);
   camera.startPreview();
   previewing = true;
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

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

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

camera.stopPreview();
 //camera.release();
 //camera = null;
 previewing = false;
}
} 
于 2013-11-03T08:48:06.103 回答