2015-05-20 13 views
5

Sto costruendo un'API per Android, in cui gli sviluppatori possono utilizzare la mia classe per scattare una foto. Ma, come posso restituire una bitmap usando onPictureTaken. Guardate il mio codice, per favore:Come restituire una bitmap usando onPictureTaken su Android?

Questo è un tasto sul mainActivity:

button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // onPictureTaken does not return nothing 
      // so, how can I get a bitmap from takePicture? 
      Bitmap mBitmap = CameraUtils.takePicture(); 
     } 
    }); 

Ed ecco la mia CameraUtils classe:

public class CameraUtils implements Camera.PictureCallback { 

private Camera mCamera = null; 
private SurfaceTexture surfaceTexture = new SurfaceTexture(0); 

public Bitmap takePicture() { 

    try { 
     mCamera = Camera.open(1); 

     Camera.Parameters params = mCamera.getParameters(); 
     List<Camera.Size> sizes = params.getSupportedPictureSizes(); 
     params.setPictureFormat(ImageFormat.JPEG); 
     params.setPictureSize(sizes.get(0).width, sizes.get(0).height); 
     mCamera.setParameters(params); 

     mCamera.setPreviewTexture(surfaceTexture); 
     mCamera.startPreview(); 

     Log.i("MyCamera", "before takePicture"); 

     new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
       mCamera.takePicture(null, null, CameraUtils.this); 
       } 
      }, 1000); 


     //how to return a bitmap from here?? 
     //how to wait for onPictureTaken?? 
     return ????? 

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

@Override 
public void onPictureTaken(byte[] data, Camera camera) { 
     Bitmap mBitmap = BitmapFactory 
       .decodeByteArray(data, 0, data.length); 

     // 
     // how to return something here?? 
     // 
     // 
     // What logic I have to do? 
     // 

     // 
     // 
     // return mBitmap; 
     // 
} 
} 

Cosa devo fare per ottenere una bitmap utilizzando la mia classe ' metodo?

Grazie.

risposta

0

Non è così. Quello che fai invece è aggiornare l'interfaccia utente in qualsiasi modo tu voglia in onPictureTaken. Non si restituisce nulla da esso.

+0

Grazie per avermi aiutato. Non voglio usare Bitmap sull'interfaccia utente. Voglio elaborare questa bitmap su altri metodi. Ad esempio: Bitmap bitmap = CameraUtils.takePicture(); CameraUtils.rotateBitmap (bitmap, dregrees); È un modo per farlo? – Caaarlos

+0

Sì, fai tutto in onPictureTaken –

+0

Ma ho molti metodi per manipolare la bitmap. Ho davvero bisogno di qualcosa per ottenere una bitmap e processare l'immagine nel modo che voglio. C'è qualche logica, diversa da fare tutto onPictureTaken? – Caaarlos

Problemi correlati