2016-09-22 4 views
0

카메라를 화면에 표시하지 않고 전면 카메라에서 사진을 촬영하는 방법. 나는 서비스 클래스가 있습니다서비스중인 카메라에서 사진 찍기

public class PhotoTakingService extends Service { 
    //Camera variables 
    //a surface holder 
    private SurfaceHolder sHolder; 
    //a variable to control the camera 
    private Camera mCamera; 
    //the camera parameters 
    private Parameters parameters; 

    boolean mPreviewRunning = false; 


    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate() { 
     super.onCreate(); 

    } 

    @Override 
    public void onStart(Intent intent,int startId) { 
     // TODO Auto-generated method stub 
     super.onStart(intent,startId); 



     mCamera = Camera.open(); 
     SurfaceView sv = new SurfaceView(getBaseContext()); 


     try { 

      Camera.Parameters p = mCamera.getParameters(); 
      mCamera.setParameters(p); 
      mCamera.startPreview(); 

      mCamera.takePicture(null,null,mPictureCallback); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     //Get a surface 
     sHolder = sv.getHolder(); 
     //tells Android that this surface will have its data constantly replaced 
     sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 


    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { 
     public void onPictureTaken(byte[] imageData,Camera c) { 
      Log.e("Callback TAG","Here in jpeg Callback"); 

      if (imageData != null) { 
       FileOutputStream outputStream = null; 
       try { 
        outputStream = new FileOutputStream("/sdcard/car_final/Image.jpg"); 
        outputStream.write(imageData); 

        // Removed the finish call you had here 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } finally { 
        if (outputStream != null) try { 
         outputStream.close(); 
        } catch (IOException ex) { 
         // TODO Auto-generated catch block 
         ex.printStackTrace(); 
        } 
       } 

      } 
     } 
    }; 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

및 주요 활동. 주된 활동에서 전화하고 싶습니다.

public class MainActivity extends Activity implements SurfaceHolder.Callback { 



private static final String TAG = MainActivity.class.getSimpleName(); 

    public static SurfaceView mSurfaceView; 
    public static SurfaceHolder mSurfaceHolder; 
    public static Camera mCamera; 
    public static boolean mPreviewRunning; 

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

     mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView); 
     mSurfaceHolder = mSurfaceView.getHolder(); 
     mSurfaceHolder.addCallback(this); 
     mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

     Button btnStart = (Button) findViewById(R.id.button1); 
     btnStart.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       Intent intent = new Intent(MainActivity.this, PhotoTakingService.class); 
       //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startService(intent); 

      } 
     }); 

     /* Button btnStop = (Button) findViewById(R.id.StopService); 
     btnStop.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       stopService(new Intent(CameraRecorder.this, RecorderService.class)); 
      } 
     });*/ 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 

    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
    } 
} 

답변

0

당신은 서비스 이용 SurfaceTexture 대신 SurfaceHolder으로 백그라운드 스레드에있을 때. 만약 당신이 구현을 찾고 있습니다 here is a opensource app 어디 배경 비디오 스트림 및 UI 비디오 스트림을 모두 구현했습니다.

+0

카메라를 보이지 않고 전면 카메라에서 캡처 한 이미지 만 필요합니다. –

+0

유사한 링크가 있으면 제공된 링크에서 아이디어를 얻을 수 있습니다. –