2014-10-22 3 views
0

미리보기 및 사진을 표시 할 수있는 Android 카메라 앱을 만들고 있습니다. 이 액티비티에는 2 개의 버튼이 구현되어 있습니다. 미리보기를 시작하고 미리보기를 중지하십시오. 사용자가 미리보기 중지 버튼을 클릭하면 앱에서 사진을 캡처합니다. 그러나 앱을 테스트 할 때 미리보기 중지와 관련된 코드는 건너 뛰고 실행되지 않는 것으로 보입니다. 아래Android 코드가 실행되지 않았습니다.

SuppressWarnings("deprecation") 
public class CameraOpened extends Activity implements SurfaceHolder.Callback{ 

    Camera camera; 
    SurfaceView surfaceView; 
    SurfaceHolder surfaceHolder; 
    boolean previewing = false; 
    static final int REQUEST_IMAGE_CAPTURE = 1; 
    int count; 
    PictureCallback rawCallback; 
    ShutterCallback shutterCallback; 
    PictureCallback jpegCallback; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_camera_opened); 

      Button buttonStartCameraPreview = (Button)findViewById(R.id.startcamerapreview); 
      Button buttonStopCameraPreview = (Button)findViewById(R.id.stopcamerapreview); 

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

      shutterCallback = new ShutterCallback() { 
       public void onShutter() { 
        Log.i("Log", "onShutter'd"); 
       } 
      }; 

      rawCallback = new PictureCallback() { 
       public void onPictureTaken(byte[] data, Camera camera) { 
        Log.d("Log", "onPictureTaken - raw"); 
       } 
      }; 

      jpegCallback = new PictureCallback() { 
       public void onPictureTaken(byte[] data, Camera camera) { 
        FileOutputStream outStream = null; 
        try { 
         String dir; 
         dir = Environment.getExternalStorageDirectory().toString() + "/DCIM/"; 
         File newdir = new File(dir); 
         newdir.mkdirs(); 

         File imageFileName = new File(newdir, "crop_"+ ".jpg"); 
         outStream = new FileOutputStream(imageFileName); 
         outStream.write(data); 
         outStream.close(); 
         Log.d("Log", "onPictureTaken - wrote bytes: " + data.length); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } finally { 
        } 
        Log.d("Log", "onPictureTaken - jpeg"); 
       } 
      }; 

      buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener(){ 

       public void onClick(View v) { 

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

      buttonStopCameraPreview.setOnClickListener(new Button.OnClickListener(){ 

       @Override 
       public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if(camera != null && previewing){ 

       camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
       previewing = false; 


       camera.stopPreview(); 
       camera.release(); 
       camera = null; 
       } 
       }}); 
    } 

    public void savePhoto(Bitmap bmp) 
    { 
     String dir; 
    dir = Environment.getExternalStorageDirectory().toString() + "/DCIM/"; 
    File newdir = new File(dir); 
    newdir.mkdirs(); 
    FileOutputStream out = null; 

    //file name 
    Calendar c = Calendar.getInstance(); 
    String date = fromInt(c.get(Calendar.MONTH)) 
       + fromInt(c.get(Calendar.DAY_OF_MONTH)) 
       + fromInt(c.get(Calendar.YEAR)) 
       + fromInt(c.get(Calendar.HOUR_OF_DAY)) 
       + fromInt(c.get(Calendar.MINUTE)) 
       + fromInt(c.get(Calendar.SECOND)); 
    File imageFileName = new File(newdir, "crop_"+ ".jpg"); 

    try 
    { 
     out = new FileOutputStream(imageFileName); 
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); 
     out.flush(); 
     out.close(); 
     out = null; 
    } catch (Exception e) 
    { 
    e.printStackTrace(); 
    } 
    } 

    public String fromInt(int val) 
    { 
    return String.valueOf(val); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.camera_opened, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @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 

    } 

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

    } 
    private void captureImage() { 
      // TODO Auto-generated method stub 
      //camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
     } 


} 

그리고 내 로그 캣은 다음과 같습니다 : 다음은 내 코드입니다

10-22 20:28:38.218: D/Camera(14375): [CTA] Camera open urrentpackagename = com.camera 
10-22 20:28:38.218: D/Camera(14375): [CTA] Camera open application != null 
10-22 20:28:38.218: D/Camera(14375): [CTA] check CTA permisson mAllowUsing = true 
10-22 20:28:39.258: I/Choreographer(14375): Skipped 61 frames! The application may be doing too much work on its main thread. 
10-22 20:28:49.318: D/Camera(14375): [CTA] Camera open urrentpackagename = com.camera 
10-22 20:28:49.318: D/Camera(14375): [CTA] Camera open application != null 
10-22 20:28:49.318: D/Camera(14375): [CTA] check CTA permisson mAllowUsing = true 
10-22 20:28:53.028: I/Choreographer(14375): Skipped 77 frames! The application may be doing too much work on its main thread. 

이 문제를 해결하는 방법에 대한 어떤 생각? 카메라 작업

+0

버튼이 표시되고 클릭 할 것입니다 희망입니까? XML은 무엇입니까? 리스너가 실행되지 않는다고 생각하는 이유는 무엇입니까? 'Log.d ("~~~", "~~~ stop")'을 다른 청취자에게 추가하는 것이 좋습니다. – 18446744073709551615

+0

버튼을 표시하고 클릭 할 수 있습니다. –

답변

관련 문제