2014-02-13 77 views
1

我不希望我的應用程序調用本機相機應用程序。我想要的只是讓我的應用程序在應用程序啓動時以編程方式拍攝照片,而無需任何用戶交互或參與。我如何使我的相機在android中以編程方式拍照?

這裏是我的代碼:

public class CameraActivity extends Activity implements SurfaceHolder.Callback{ 


    Camera camera; 
    PictureCallback rawCallback; 
    ShutterCallback shutterCallback; 
    PictureCallback jpegCallback; 
    ImageView view; 

    boolean inPreview = false; 

    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     view = (ImageView)findViewById(R.id.pic); 

     Log.e("Camera Status: ",String.valueOf(checkCameraHardware(getApplicationContext()))); 
     Log.e("Number Of Cameras: ",String.valueOf(Camera.getNumberOfCameras())); 

     start_camera(); 
     captureImage(); 

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

      /** Handles data for jpeg picture */ 
      shutterCallback = new ShutterCallback() { 
       public void onShutter() { 
        Log.i("Log", "onShutter'd"); 
       } 
      }; 

      jpegCallback = new PictureCallback() { 
       public void onPictureTaken(byte[] data, Camera camera) { 
        //Set the Image to ImageView 
        Bitmap bitmapPicture 
        = BitmapFactory.decodeByteArray(data, 0, data.length); 
        //view.setImageBitmap(bitmapPicture); 

       //Save the Image to SD Card 
        FileOutputStream outStream = null; 
        try { 
         outStream = new FileOutputStream(String.format(
           "/sdcard/%d.jpg", System.currentTimeMillis())); 
         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"); 



        /** 
        * The two lines below are used to refresh the Surface View 
        * It works quickly then the general refresh by Default 
        **/ 
        stop_camera(); 
        start_camera(); 
       } 
      }; 

     //start_camera(); 

    } 


    /** Check if this device has a camera */ 
    private boolean checkCameraHardware(Context context) { 
     if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ 
      // this device has a camera 
      return true; 
     } else { 
      // no camera on this device 
      return false; 
     } 
    } 

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


    public static Camera getCameraInstance(){ 
     Camera c = null; 
     try { 
      c = Camera.open(); // attempt to get a Camera instance 
     } 
     catch (Exception e){ 
      // Camera is not available (in use or does not exist) 
     } 
     return c; // returns null if camera is unavailable 
    } 


     private void start_camera() 
     { 
      try{ 
       camera = Camera.open(); 
       Log.e("Camera Open Status: ", "Camera opened successfully"); 
      }catch(RuntimeException e){ 
       Log.e("Camera Initialization:", "init_camera: " + e); 
       return; 
      } 
      Camera.Parameters param; 
      param = camera.getParameters(); 
      //modify parameter 
      param.setPreviewFrameRate(20); 
      param.setPreviewSize(176, 144); 
      camera.setParameters(param); 
      try { 
       //camera.setPreviewDisplay(surfaceHolder); 
       camera.startPreview(); 
       //camera.takePicture(shutter, raw, jpeg) 
      } catch (Exception e) { 
       Log.e("Camera Initialization:", "init_camera: " + e); 
       return; 
      } 
      inPreview=true; 
     } 


     private void captureImage() { 
      // TODO Auto-generated method stub 
      AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
      mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true); 

      camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
      Log.e("Picture Status: ","picture taken successfully"); 
      final Handler handler = new Handler(); 
      Timer t = new Timer(); 
      t.schedule(new TimerTask() { 
       public void run() { 
        handler.post(new Runnable() { 
         public void run() { 
         AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
         mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false); 
           } 
          }); 
         } 
       }, 1000); 


     } 

     private void stop_camera() 
     { 
      camera.stopPreview(); 
      camera.release(); 
      inPreview = false; 
     } 


    @Override 
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { 
     // TODO Auto-generated method stub 

    } 


    @Override 
    public void surfaceCreated(SurfaceHolder arg0) { 
     // TODO Auto-generated method stub 

    } 


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

    } 

} 

enter image description here

+0

究竟什麼是你的問題在此代碼?我已經創建了庫,你可以檢查出來https://github.com/girishnair12345/Girish-Camera-Library-Project –

+0

相機不拍照。另外我得到一個空指針異常。 –

+0

你在哪裏得到NPE? post ur logcat。 – Piyush

回答

1

下載項目形式here 複製CameraLibrary到工作區

這些添加到您的清單

<uses-sdk 
    android:minSdkVersion="10" 
    android:targetSdkVersion="10" /> 
<uses-features android:name="android.hardware.camera"/> 
<uses-permission android:name="android.permission.CAMERA"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

<activity android:name="com.girish.cameraLibrary.CameraClass"></activity> 

現在,在您的活動實現com.girish.cameraLibrary.OnPictureTaken接口並重寫pictureTaken(Bitmap bitmap, File file)方法,它可提供與位圖和文件

立即創建和CustomCamera

private CustomCamera mCustomCamera; 

mCustomCamera = new CustomCamera(MainActivity.this); 
mCustomCamera.setPictureTakenListner(this); 

//To start the custom back camera use this 
mCustomCamera.startCamera(); 
//To send an intent to applications who are listening to Camera request 
mCustomCamera.sendCameraIntent(); 
//To start the front camera use this 
mCustomCamera.startCameraFront(); 

的情況下的,現在你可以訪問使用pictureTaken(Bitmap bitmap, File file)圖像方法,所以只使用

imageview.setImageBitmap(bitmap); 

編輯

假設你的主類被稱爲MainActivity這樣

public class MainActivity extends Activity implements OnPictureTaken { 

..... 
..... 
..... 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
..... 
..... 
..... 


//This is where you get the picture 
@Override 
public void pictureTaken(Bitmap bitmap, File file) { 
    imgCapture.setImageBitmap(bitmap); 
    txtPath.setText(file.getAbsolutePath()); 
} 

寫檢查樣本文件提到here

+0

'startCameraInbuit'是你的回答還是圖書館的錯字? – Basic

+0

它不是一種類型,它發送所有正在聽相機的人的意圖。我需要更改名稱 –

+1

「排字錯誤」是指方法名稱中的拼寫錯誤 - 拼寫錯誤。我認爲它應該是'startCameraInbuilt'(注意「L」) – Basic

相關問題