Tome una foto con la cámara y obtenga bitmap

QUE NO ES UN DUPLICADO: PIDO ESTA FUNCIÓN !!!! SIN INTERACCIÓN DEL USUARIO !!!!

Estoy trabajando en una aplicación para Android que está utilizando la cámara frente a frente para simular un espejo para el usuario. Necesito la aplicación para tomar una foto cada 5 segundos como un mapa de bits de forma automática! Sin la interacción del usuario !, Que me gusta combinar con otro mapa de bits más adelante.

La parte difícil para mí: ¿Cómo tomar una foto y obtenerla como un mapa de bits?

He probado varias soluciones, pero ninguna estaba bien todavía.

Utilicé el siguiente código para mostrar la alimentación de la cámara en vivo en un fondo de diseño y el botón guarda la imagen como jpeg. Trate de modificarlo como desee: Puede descargar todo el proyecto de prueba aquí para que pueda probarlo rápido: http://www.4shared.com/rar/v-ZQPybcce/Test.html

— >> La diferencia de este código de otras personas con intentos es que toma la imagen automáticamente sin abrir la aplicación de cámara que hace que la aplicación se vea mejor 🙂 <—-

 package com.mreprogramming.test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.Toast; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Bitmap.CompressFormat; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.os.Bundle; import android.os.Environment; import android.preference.PreferenceManager; public class CameraActivity extends Activity implements SurfaceHolder.Callback{ protected static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0; private SurfaceView SurView; private SurfaceHolder camHolder; private boolean previewRunning; private Button button1; final Context context = this; public static Camera camera = null; private ImageView camera_image; private Bitmap bmp,bmp1; private ByteArrayOutputStream bos; private BitmapFactory.Options options,o,o2; private FileInputStream fis; ByteArrayInputStream fis2; private FileOutputStream fos; private File dir_image2,dir_image; private RelativeLayout CamView; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera); CamView = (RelativeLayout) findViewById(R.id.camview); SurView = (SurfaceView)findViewById(R.id.sview); camHolder = SurView.getHolder(); camHolder.addCallback(this); camHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); button1 = (Button)findViewById(R.id.button_1); camera_image = (ImageView) findViewById(R.id.camera_image); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { button1.setClickable(false); button1.setVisibility(View.INVISIBLE); //<-----HIDE HERE camera.takePicture(null, null, mPicture); } }); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(previewRunning){ camera.stopPreview(); } Camera.Parameters camParams = camera.getParameters(); Camera.Size size = camParams.getSupportedPreviewSizes().get(0); camParams.setPreviewSize(size.width, size.height); camera.setParameters(camParams); try{ camera.setPreviewDisplay(holder); camera.startPreview(); previewRunning=true; }catch(IOException e){ e.printStackTrace(); } } public void surfaceCreated(SurfaceHolder holder) { try{ camera=Camera.open(); }catch(Exception e){ e.printStackTrace(); Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show(); finish(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera=null; } public void TakeScreenshot(){ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); int nu = preferences.getInt("image_num",0); nu++; SharedPreferences.Editor editor = preferences.edit(); editor.putInt("image_num",nu); editor.commit(); CamView.setDrawingCacheEnabled(true); CamView.buildDrawingCache(true); bmp = Bitmap.createBitmap(CamView.getDrawingCache()); CamView.setDrawingCacheEnabled(false); bos = new ByteArrayOutputStream(); bmp.compress(CompressFormat.JPEG, 100, bos); byte[] bitmapdata = bos.toByteArray(); fis2 = new ByteArrayInputStream(bitmapdata); String picId=String.valueOf(nu); String myfile="MyImage"+picId+".jpeg"; dir_image = new File(Environment.getExternalStorageDirectory()+ File.separator+"My Custom Folder"); dir_image.mkdirs(); try { File tmpFile = new File(dir_image,myfile); fos = new FileOutputStream(tmpFile); byte[] buf = new byte[1024]; int len; while ((len = fis2.read(buf)) > 0) { fos.write(buf, 0, len); } fis2.close(); fos.close(); Toast.makeText(getApplicationContext(), "The file is saved at :/My Custom Folder/"+"MyImage"+picId+".jpeg",Toast.LENGTH_LONG).show(); bmp1 = null; camera_image.setImageBitmap(bmp1); camera.startPreview(); button1.setClickable(true); button1.setVisibility(View.VISIBLE);//<----UNHIDE HER } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private PictureCallback mPicture = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { dir_image2 = new File(Environment.getExternalStorageDirectory()+ File.separator+"My Custom Folder"); dir_image2.mkdirs(); File tmpFile = new File(dir_image2,"TempImage.jpg"); try { fos = new FileOutputStream(tmpFile); fos.write(data); fos.close(); } catch (FileNotFoundException e) { Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show(); } catch (IOException e) { Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show(); } options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; bmp1 = decodeFile(tmpFile); bmp=Bitmap.createScaledBitmap(bmp1,CamView.getWidth(), CamView.getHeight(),true); camera_image.setImageBitmap(bmp); tmpFile.delete(); TakeScreenshot(); } }; public Bitmap decodeFile(File f) { Bitmap b = null; try { // Decode image size o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; fis = new FileInputStream(f); BitmapFactory.decodeStream(fis, null, o); fis.close(); int IMAGE_MAX_SIZE = 1000; int scale = 1; if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) { scale = (int) Math.pow( 2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5))); } // Decode with inSampleSize o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; fis = new FileInputStream(f); b = BitmapFactory.decodeStream(fis, null, o2); fis.close(); } catch (IOException e) { e.printStackTrace(); } return b; } } 

Esta es la camera.xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/camview"> <SurfaceView android:id="@+id/sview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <ImageView android:id="@+id/camera_image" android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="@string/app_name" /> <Button android:id="@+id/button_1" android:layout_width="20dp" android:layout_height="20dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> 

Agregue esto a su manifiesto:

 <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

También en el manifiesto añada lo siguiente a la pestaña de actividad "CameraActivity" para asegurarse de que su actividad siempre estará en la orientación horizontal porque de lo contrario sosteniendo el teléfono en protrait (vertical) a menos que cambie el código invertirá la relación de aspecto de la imagen y severamente Distorsionarla.

 <activity android:name="com.mreprogramming.test.CameraActivity" android:label="@string/app_name" android:screenOrientation="landscape" > <-------ADD THIS ---!!!!! <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

Y guárdelo como styles.xml para hacer su diseño a pantalla completa

 <resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="android:Theme.NoTitleBar.Fullscreen"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="android:Theme.NoTitleBar.Fullscreen"> <item name="android:windowFullscreen">true</item> <item name="android:windowNoTitle">true</item> </style> 

En mi aplicación quiero que la imagen para capturar todas las vistas de la distribución no sólo la alimentación de la cámara como este: Introduzca aquí la descripción de la imagen

El código de prueba que he publicado aquí oculta el botón de captura para que no aparezca en tu foto. Si tienes más vistas en tu aplicación y no las quieres mostrar en la foto, o bien ocúltelas durante la captura (consulta el código para saber dónde ocultarlas) o edita el código.

——> Para resumir mi mensaje este código puede hacer la captura básica y guardar un jpeg, pero si quieres imágenes profesionales que necesita para editar un poco. Buena suerte

No sé si esta solución es lo que quieres. Sin embargo, lo básico para tomar una imagen como mapa de bits debe ser así:

  private static final int CAMERA_REQUEST = 1888; // field private void takePicture(){ //you can call this every 5 seconds using a timer or whenever you want Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) { Bitmap picture = (Bitmap) data.getExtras().get("data");//this is your bitmap image and now you can do whatever you want with this imageView.setImageBitmap(picture); //for example I put bmp in an ImageView } } 

No olvide establecer permiso para la cámara en su Manifiesto:

 <uses-feature android:name="android.hardware.camera" /> 

En primer lugar, debe tomar la fotografía utilizando IMAGE_CAPTURE Intent

Entonces usted querrá crear un archivo temporal para almacenar la foto en para que no use toda la memoria en el teléfono.

Después de eso, tome el mapa de bits utilizando android.provider.MediaStore.Images.Media.getBitmap() y seleccione el archivo temporal.

Aquí hay un ejemplo de código completo sobre cómo hacerlo:

Android Camera Intent: ¿cómo obtener una foto de tamaño completo?

  • Cómo capturar imágenes de baja resolución con la cámara con Android
  • Resumen: Tome una foto utilizando Intención de la cámara y muestre la foto con la orientación correcta (funciona con todos los dispositivos)
  • La cámara PhoneGap reinicia la aplicación
  • Problema de orientación de la cámara en Android
  • Error al ejecutar la muestra de la cámara de teléfono
  • Graba, guarda y reproduce un video en Android
  • ¿Quieres encontrar la longitud focal primero entonces la distancia de la cara detectada en tiempo real utilizando opencv android
  • FileProvider no funciona con la cámara
  • S3 (GT-I9300) Bug de la previsualización de la cámara
  • Cómo saber qué aplicación (proceso) está utilizando la cámara en Android
  • Transmisión de vídeo en directo desde la cámara de Android al servidor
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.