2016-07-18 70 views
0

我是cordova的新手。致力於設備項目。如何打開默認相機並自動拍照android

我的要求是,

  • BLE裝置上首先點擊纔可以打開攝像頭。
  • 下一個相應的點擊拍照。

AutomaticPhotoActivity.java

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.SurfaceTexture; 
import android.hardware.Camera; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.os.Environment; 
import android.util.Log; 
import android.view.SurfaceView; 
import android.view.View; 
import android.widget.TextView; 

public class AutomaticPhotoActivity extends Activity{ 
    private Camera camera; // camera object 
    private TextView textTimeLeft; // time left field 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     textTimeLeft=(TextView)findViewById(R.id.textTimeLeft); // make time left object 

     /* Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(intent, 0);*/ 
      camera = Camera.open(0); 
     try { 
      SurfaceView view = new SurfaceView(this); 


      camera.setPreviewDisplay(view.getHolder()); // feed dummy surface to surface 
      SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE); 
      camera.setPreviewTexture(st); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    Camera.PictureCallback jpegCallBack=new Camera.PictureCallback() { 
     public void onPictureTaken(byte[] data, Camera camera) { 
      // set file destination and file name 
      File destination=new File(Environment.getExternalStorageDirectory(),"myPicture.jpg"); 
      try { 
       Bitmap userImage = BitmapFactory.decodeByteArray(data, 0, data.length); 
       // set file out stream 
       FileOutputStream out = new FileOutputStream(destination); 
       // set compress format quality and stream 
       userImage.compress(Bitmap.CompressFormat.JPEG, 90, out); 

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

     } 
    }; 
// onclick off button wait for 5seconds then take phot 
    public void startTimer(View v){ 

     // 5000ms=5s at intervals of 1000ms=1s so that means it lasts 5 seconds 
     new CountDownTimer(5000,1000){ 

      @Override 
      public void onFinish() { 
       // count finished 
       textTimeLeft.setText("Picture Taken"); 
       camera.takePicture(null, null, null, jpegCallBack); 
      } 

      @Override 
      public void onTick(long millisUntilFinished) { 
       // every time 1 second passes`enter code here` 
       textTimeLeft.setText("Seconds Left: "+millisUntilFinished/1000); 
      } 

     }.start(); 
    } 

} 

我可以使用此代碼,但沒有默認打開相機中的照片。 如何在不使用surfaceview的情況下打開相機後拍攝照片。因爲我正在做所有我的設計在HTML CSS,沒有XML。 請幫助我。

UPDATE

這兩個答案是用表面view.In我來說,我需要打開默認的攝像頭應用程序,並具有拍照。像自拍棒如何工作。

簡單: 如何從默認相機應用程序自動拍照。

+0

[徑而無需用戶交互自動相片](http://stackoverflow.com/questions/9752730/take-a-photo-automatically-without-user-interaction) –

+0

重複的問題的可能的複製看到這個[自動拍照無需用戶交互](http://stackoverflow.com/questions/9752730/take-a-photo-automatically-without-user-interaction) – FarshidABZ

+0

沒有這個問題是不同於這些答案。我想無需用戶交互即可從默認相機應用拍攝照片。 –

回答

0

試試這個code.It將啓動默認相機,你會得到回調在活動結果後點擊保存圖片按鈕。

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
     startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
    } 

REQUEST_IMAGE_CAPTURE將任何int值。

https://developer.android.com/training/camera/photobasics.html

+0

感謝您的答覆。但這一個我試過了,它只是啓動默認的相機應用程序,但不會自動拍照。 –

相關問題