2016-03-13 15 views
6

Sono nuovo in Android camera2 APi. Ho appena spostato tutto il mio progetto sul nuovo Camera2 Api. Ho usato il Camera2Basic example come un punto di partenza.android camera2 maniglia zoom

Im ora cercando la maniglia dello zoom aggiungendo questo:

public boolean onTouchEvent(MotionEvent event) { 
    try { 
     CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); 
     CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId); 
     float maxZoom = (characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM))*10; 

     Rect m = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE); 
     int action = event.getAction(); 
     float current_finger_spacing; 

     if (event.getPointerCount() > 1) { 
      // Multi touch logic 
      current_finger_spacing = getFingerSpacing(event); 

      if(finger_spacing != 0){ 
       if(current_finger_spacing > finger_spacing && maxZoom > zoom_level){ 
        zoom_level++; 

       } 
       else if (current_finger_spacing < finger_spacing && zoom_level > 1){ 
        zoom_level--; 

       } 
       int minW = (int) (m.width()/maxZoom); 
       int minH = (int) (m.height()/maxZoom); 
       int difW = m.width() - minW; 
       int difH = m.height() - minH; 
       int cropW = difW /100 *(int)zoom_level; 
       int cropH = difH /100 *(int)zoom_level; 
       cropW -= cropW & 3; 
       cropH -= cropH & 3; 
       Rect zoom = new Rect(cropW, cropH, m.width() - cropW, m.height() - cropH); 
       mPreviewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom); 
      } 
      finger_spacing = current_finger_spacing; 
     } 
     else{ 
      if (action == MotionEvent.ACTION_UP) { 
       //single touch logic 
      } 
     } 

     try { 
      mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, 
        null); 
     } 
     catch (CameraAccessException e) { 
      e.printStackTrace(); 
     } 
     catch (NullPointerException ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 
    catch (CameraAccessException e) 
    { 
     throw new RuntimeException("can not access camera.", e); 
    } 

    return true; 
} 

E questo:

private float getFingerSpacing(MotionEvent event) { 
    float x = event.getX(0) - event.getX(1); 
    float y = event.getY(0) - event.getY(1); 
    return FloatMath.sqrt(x * x + y * y); 
} 

Ma dopo ho catturato, il risultato dell'immagine è senza lo zoom. Come posso farcela? Grazie a tutti.

Aggiornamento necessario aggiungere captureBuilder.set (CaptureRequest.SCALER_CROP_REGION, zoom); Per catturare il metodoStillPicture().

+0

hai aggiustato? –

+0

Sì, aggiunta la mia soluzione sotto –

+0

qual è il metodo captureStillPicture() ??? – user3819810

risposta

4

È necessario aggiungere captureBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom); Per acquisire il metodoStillPicture().

2

È stato impostato lo SCALER_CROP_REGION su CaptureRequestBuilder per l'uscita ricorrente della telecamera di anteprima. Devi solo aggiungere la stessa proprietà della regione di coltura allo CaptureRequestBuilder che utilizza il Surface JPEG Surface di ImageSaver come output e dovresti essere tutto pronto.

+0

Puoi essere più spesifico per favore? In quale metodo dovrebbe essere visualizzato? –

+0

@EliElezra è necessario chiamare set (CaptureRequest.SCALER_CROP_REGION) nel builder inviato alla foto quando lo si acquisisce. Il builder contiene tutte le informazioni sull'anteprima che stai guardando e la foto che verrà salvata. Puoi modificare i parametri di impostazione delle anteprime e delle foto nel tuo builder. Puoi vedere maggiori informazioni in: http://developer.android.com/intl/es/reference/android/hardware/camera2/CameraCharacteristics.html PD: ti lascio un post qui sotto di come l'ho fatto, guarda. –

0

Basta impostare SCALER.CROP.REGION per captureBuilder. Puoi farlo in questo modo:

yourCapturebuilder.(CaptureRequest.SCALER_CROP_REGION, newZoom); 

In altro modo, se si desidera mantenere lo Zoom in una preferenza. Vi suggerisco di fare qualcosa di simile: Salvare il Rect in una preferenza come una stringa e poi recuperarlo per usarlo, o chiamare ogni volta che si apre la fotocamera:

Preferences.edit().putString(CameraSettings.KEY_ZOOM,newZoom.toString()); 

poi chiamato questo metodo per impostare lo zoom dinamicamente:

public boolean setZoomValue(CaptureRequest.Builder builder) { 
     Log.i(TAG,"zoom preference value " + mPreferences.getString(CameraSettings.KEY_ZOOM,null)); 
     String rawZoomValue = mPreferences.getString(CameraSettings.KEY_ZOOM, null); 
     if (rawZoomValue == null) 
      return false; 
     rawZoomValue = rawZoomValue.replaceAll("[Rect() ]", ""); 
     String[] rectZoomList = rawZoomValue.split(",|\\-|\\)|\\("); 
     Rect zoomValue = new Rect(Integer.parseInt(rectZoomList[0]), Integer.parseInt(rectZoomList[1]), Integer.parseInt(rectZoomList[2]),Integer.parseInt(rectZoomList[3])); 
     builder.set(CaptureRequest.SCALER_CROP_REGION, zoomValue); 
     Log.i(TAG, "Zoom applied: " + zoomValue); 
     return true; 

    } 

Spero che ti possa aiutare!