2016-07-18 13 views
0

Ich bin neu in cordova. Arbeiten am Projekt ble device.So öffnen Sie Standard-Kamera und ein Foto automatisch in Android

Meine Forderung ist, sollte

  • Klicken Sie zuerst auf ble Gerät die Kamera öffnen.
  • nächste entsprechende Klicks, um Fotos zu machen.

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(); 
    } 

} 

ich ein Foto aufnehmen können diesen Code verwenden, aber ohne Standard-Kamera geöffnet. Wie man ein Foto macht, nachdem ich die Kamera geöffnet habe, ohne Oberflächen zu verwenden. Als ich mache alle meine Entwürfe in html css, kein xml. bitte hilf mir.

UPDATE

Die beiden Antwort Oberfläche benötigen view.In meinem Fall wurde unter Verwendung i app Standard-Kamera zu öffnen und um ein Foto aufzunehmen hat. Wie funktioniert Selfiestick?

in einfach: wie automatisch Foto von Standard-Kamera-App.

+0

Mögliche Duplikat [Nehmen Sie ein Foto automatisch ohne Zutun des Benutzers] (http://stackoverflow.com/questions/9752730/take-a-photo-automatically-without-user-interaction) –

+0

Doppelte Frage sieh dies [Nimm ein Foto automatisch ohne Benutzerinteraktion] (http://stackoverflow.com/questions/9752730/take-a-photo-automatisch-ohne-benutzer-interaktion) – FarshidABZ

+0

Nein, diese Frage ist anders als diese Antworten.Ich will um ein Foto von der Standard-Kamera-App ohne Benutzerinteraktion zu machen. –

Antwort

0

Versuchen Sie diesen Code.Es startet Standard-Kamera und Sie erhalten Rückruf in Aktivität Ergebnis nach Klick auf Speichern Bild-Taste.

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

REQUEST_IMAGE_CAPTURE wird jeder int-Wert sein.

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

+0

danke für die Antwort.aber diese hier habe ich schon versucht, es startet nur die Standard-Kamera-App, aber nicht automatisch Foto machen. –