2014-06-10 3 views
3

그래서 정기적 인 시간 간격으로 여러 장의 사진을 찍으려고합니다. 그러나 표면 사진을 미리보기 한 후 "takePicture Failed" Exception이 나옵니다.일정한 간격으로 사진 찍기 (Android Camera API)

는 여기에 버튼을 누를 때 호출되는 내 takePictures() 방법입니다 :

public void takePictures() { 
     if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 
      camera = Camera.open(); 

      if (camera != null) { 
       try { 
        camera.setPreviewDisplay(surfaceView.getHolder()); 
        camera.startPreview(); 

        camera.takePicture(null, null, new CustomPictureCallbcak(this, cacheDir, imageView, 3, 5000)); 
       } 
       catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     else { 
      Toast.makeText(this, "No camera found.", Toast.LENGTH_SHORT).show(); 
     } 
    } 

을 그리고 여기 CustomPictureCallbackonPictureTaken() 방법입니다 : 알렉산더 Lidtke 제안으로

@Override 
    public void onPictureTaken(byte[] data, Camera camera) { 
     //get date info for file name 
     SimpleDateFormat sdf = new SimpleDateFormat("ddmmyyyyhhmmss"); 
     String date = sdf.format(new Date()); 
     String fileDir = createImageFileName(date); 

     //write the image to cache 
     writeImageToCache(fileDir, data); 

     //display file name in a toast notification 
     Toast.makeText(c, fileDir, Toast.LENGTH_SHORT).show(); 

     //show picture on imageview 
     imageView.setImageBitmap(BitmapFactory.decodeByteArray(data, 0, data.length)); 

     //retake images 
     this.camera = camera; 
     while (numOfImagesAlreadyTaken <= numOfImages) { 
      Thread thread = new Thread() { 

       @Override 
       public void run() { 
        // TODO Auto-generated method stub 
        super.run(); 
        try { 
         numOfImagesAlreadyTaken++; 
         CustomPictureCallbcak.this.camera.stopPreview(); 
         sleep(delay); 
         CustomPictureCallbcak.this.camera.takePicture(null, null, CustomPictureCallbcak.this); 
        } 
        catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      }; 

      thread.start(); 
     } 

      Toast.makeText(c, "Total images taken: " + numOfImagesAlreadyTaken, Toast.LENGTH_SHORT).show(); 

      //release camera 
      camera.release(); 
      camera = null; 

    } 
+0

어디에서 예외가 발생합니까? –

+0

@AleksanderLidtke, 첫 번째 그림을 찍은 직후, 기본적으로 새 스레드가 시작될 때 기본적으로 수행됩니다. –

+1

맞아요. 왜'OnPictureTaken' 안에 사진을 찍는 스레드를 만드나요? 'thread'를 만들고'while' 루프를'takePictures'에 넣고 콜백 메소드를 비교적 간단하게 두는 것이 더 쉽고 깨끗하지 않습니까? 나는 여러분이 이것을하려고하는 것과 정확히 똑같은 일을하는 코드를 작성 했으므로이 코드는 git-go에서 잘 작동했다. –

답변

3

, 나는 하나의 스레드를 생성 내 takePictures() 메서드 안에 while 루프를 넣으십시오.

public void takePictures(final int numOfPictures, final int delay) { 

    if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 

     Thread thread = new Thread() { 
      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       super.run(); 
       while (numOfPicturesAlreadyTaken <= numOfPictures) { 
        try { 
         camera = Camera.open(); 
         camera.setPreviewDisplay(surfaceView.getHolder()); 
         camera.startPreview(); 
         camera.takePicture(null, null, new CustomPictureCallbcak(MainActivity.this, cacheDir, imageView)); 
         numOfPicturesAlreadyTaken++; 
         sleep(delay); 
        } 
        catch (Exception e) { 
         e.printStackTrace(); 
         Log.d("TEST", e.getMessage()); 
        } 
       } 
      } 
     }; 
     thread.start(); 
    } 
    else { 
     Toast.makeText(this, "No camera found.", Toast.LENGTH_SHORT).show(); 
    } 
}