Jump to content

Dessiner par dessus une SurfaceView


Guest

Recommended Posts

Salut à tous!

Voilà mon problème. Je suis en train de développer mon propre module de prise de photos, et j'ai suivi quelques tutos là dessus. J'ai donc un bout de programme qui m'affiche au travers d'une SurfaceView la prévisualisation issue de l'appareil photo, et lorsque je clique sur la molette, la photo est prise.

Donc, jusqu'ici, aucun problème. Mais ce que j'aimerais faire, c'est pouvoir afficher par exemple un petit curseur qui change de couleur lorsque l'auto focus est fixé, ou encore affiché une icone si le GPS à trouvé des coordonées de position... Et tout ça en "Overlayer" (par dessus la SurfaceView).

Avez vous des pistes ?

Voici mon code:


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.ExifInterface;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

public class CameraActivity extends Activity implements SurfaceHolder.Callback,
Camera.AutoFocusCallback, Camera.PictureCallback, LocationListener{

   Camera mCamera;
   double latitude ;
   double longitude ;
   double latDegree;
   double lonDegree;
   double latMinute;
   double lonMinute;
   double latSecond;
   double lonSecond;    
   boolean mPreviewRunning = false;

   public void onCreate(Bundle icicle){
       super.onCreate(icicle);

       requestWindowFeature(Window.FEATURE_NO_TITLE);
       //this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
           //    WindowManager.LayoutParams.FLAG_FULLSCREEN);  
       getWindow().setFormat(PixelFormat.TRANSLUCENT);  
       setContentView(R.layout.main);

       // Check if the directory where our photos are saved exists:
       File path = new File(Environment.getExternalStorageDirectory()+"/MyDir");
       if (!path.exists()){
           path.mkdirs();
           Log.i("MKDIR", "DOSSIER CREE");
       }

       // Launch GPS scan
       LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
       lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 100.0f, this);
       //lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000L, 100.0f, this);

       // Display the camera streaming and catch "take picture" button
       mSurfaceView = (SurfaceView) findViewById(R.id.surface);  

       mSurfaceHolder = mSurfaceView.getHolder();
       mSurfaceHolder.addCallback(this);
       mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
       }

   public void surfaceCreated(SurfaceHolder holder){
       mCamera = Camera.open();
   }

   public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){
       if (mPreviewRunning)
       {
           mCamera.stopPreview();
       }

       Camera.Parameters p = mCamera.getParameters();
   //    p.setPreviewSize(w, h);
       p.setPictureFormat(PixelFormat.JPEG);
       p.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);

       // If we aren't landscape (the default), tell the camera we want portrait mode
       if (this.getResources().getConfiguration().orientation !=
           Configuration.ORIENTATION_LANDSCAPE)
       {
           p.setRotation(90);
       }

       mCamera.setParameters(p);
       try
       {
           mCamera.setPreviewDisplay(holder);
       } catch (IOException e)
       {
           e.printStackTrace();
       }
       mCamera.startPreview();
       mPreviewRunning = true;
   }

   public void surfaceDestroyed(SurfaceHolder holder){
       mCamera.stopPreview();
       mPreviewRunning = false;
       mCamera.release();
   }

   private SurfaceView mSurfaceView;
   private SurfaceHolder mSurfaceHolder;

   public void onAutoFocus(boolean success, Camera camera){
       camera.takePicture(null, null, this);
   }



   public void onPictureTaken(byte[] data, Camera camera){  
       try
       {
           FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory()+"/MyDir/"+"test.jpg");
           fos.write(data);
           fos.close();

           ExifInterface exif = new ExifInterface(Environment.getExternalStorageDirectory()+"/MyDir/"+"test.jpg");
           exif.setAttribute(ExifInterface.TAG_ORIENTATION, "1");
           exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
           exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, ""+latDegree+"/1,"+latMinute +"/1,"+latSecond+"/1");
           exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
           exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, ""+lonDegree+"/1," +lonMinute +"/1,"+lonSecond+"/1");      
           exif.saveAttributes();


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

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event){
   if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER )
   {

       mCamera.autoFocus(this);
       return true;
   }
   return super.onKeyDown(keyCode, event);
   }

   @Override
   public void onLocationChanged(Location location) {
       if (location != null) {           
           // Get latitude and longitude from the GPS
           latitude = location.getLatitude();
           longitude = location.getLongitude();

           // Get Latitude in Degree/Minute/Second
           LatLonConvert latConvert = new LatLonConvert(latitude);          
           latDegree = latConvert.getDegree();
           latMinute = latConvert.getMinute();
           latSecond = latConvert.getSecond();

           // Get Longitude in Degree/Minute/Second
           LatLonConvert lonConvert = new LatLonConvert(longitude);          
           lonDegree = lonConvert.getDegree();
           lonMinute = lonConvert.getMinute();
           lonSecond = lonConvert.getSecond();           
       }  
   }

   @Override
   public void onProviderDisabled(String provider) {
       // TODO Auto-generated method stub

   }

   @Override
   public void onProviderEnabled(String provider) {
       // TODO Auto-generated method stub

   }

   @Override
   public void onStatusChanged(String provider, int status, Bundle extras) {
       // TODO Auto-generated method stub

   }  
}

Et pour le layout:


<?xml version="1.0" encoding="utf-8"?>
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/surface"
   />


Merci d'avance !

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...