2016-03-30 13 views
0

Projemde özel kamera uygulaması var. Cihazın arka kamerasını kullandığımda iyi çalışıyor. Dikey modda veya ters dikey modda ön kamera ile fotoğraf çekerken, görüntü başaşağı döndürür. Manzara modunda, her iki kamera da iyi çalışıyor.Ön kamera ile çekilen resim başaşağı

Ben SensorEventListener uygulanacak ve yakalanan dosyada ExifInterface.TAG_ORIENTATION ayarlamak amacıyla SensorChanged geri aramasında orientation değerini hesaplayarak var. kullanıcı ekranı döndüğü zaman ekranda mevcut simgeler rotasyon animasyon ekliyorum çünkü

public void onSensorChanged(SensorEvent event) { 
     synchronized (this) { 
      if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 


       if (event.values[0] < 4 && event.values[0] > -4) { 
        if (event.values[1] > 0 && orientation != ExifInterface.ORIENTATION_ROTATE_90) { 
         // UP 
         orientation = ExifInterface.ORIENTATION_ROTATE_90; 
        } else if (event.values[1] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_270) { 
         // UP SIDE DOWN 
         orientation = ExifInterface.ORIENTATION_ROTATE_270; 
        } 
       } else if (event.values[1] < 4 && event.values[1] > -4) { 
        if (event.values[0] > 0 && orientation != ExifInterface.ORIENTATION_NORMAL) { 
         // LEFT 
         orientation = ExifInterface.ORIENTATION_NORMAL; 
        } else if (event.values[0] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_180) { 
         // RIGHT 
         orientation = ExifInterface.ORIENTATION_ROTATE_180; 
        } 
       } 

      } 

     } 
    } 

Ben SensorEventListener kullanıyorum nedeni budur. İşte benim PictureCallback geçerli: hata ayıklama için

private Camera.PictureCallback mPictureCallBack = new Camera.PictureCallback() { 

     public void onPictureTaken(byte[] data, Camera camera) { 
      mCamera.stopPreview(); 

      File pictureFile = new File(fileName); 

      try { 
       FileOutputStream purge = new FileOutputStream(pictureFile); 
       purge.write(data); 
       purge.close(); 
      } catch (FileNotFoundException e) { 

      } catch (IOException e) { 

      } 

      // Adding Exif data for the orientation. 
      try { 

       exif = new ExifInterface(fileName); 
       exif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + orientation); 
       exif.saveAttributes(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
    }; 

, davada ExifInterface.ORIENTATION_ROTATE_90 için orientation değeri exif.setAttribute() zaman ExifInterface.ORIENTATION_ROTATE_270 ve tersi, bu iyi çalışıyor. Sorum şu: Tüm aygıtlara en iyi şekilde uyacak şekilde bu sorunu çözmek için en iyi ve kesin çekim nedir.

Note : Bu süreçte Bitmap kullanmıyorum. Bir dosyaya data yazıyorum ve yakalanan görüntüyü göstermek için tekrar başka bir etkinliğe gönderiyorum.

Her türlü yardım büyük beğeni topluyor.

cevap

0

Bu problemi birkaç defa önce yaşadım. Yaptığım şey, cepheden bir kamera kullandığınızda cihazın negatif dönüşüne dikkat ederek görüntüyü döndürmekti. Yani rotasyon dönüştürmek için böyle bir şey yapmanız gerekir:

public static Bitmap rotateImage(Bitmap imageToOrient, int degreesToRotate) { 
    Bitmap result = imageToOrient; 
    try { 
     if (degreesToRotate != 0) { 
      Matrix matrix = new Matrix(); 
      matrix.setRotate(degreesToRotate); 
      result = Bitmap.createBitmap(
        imageToOrient, 
        0, 
        0, 
        imageToOrient.getWidth(), 
        imageToOrient.getHeight(), 
        matrix, 
        true); 
     } 
    } catch (Exception e) { 
     if (Log.isLoggable(TAG, Log.ERROR)) { 
      Log.e(TAG, "Exception when trying to orient image", e); 
     } 
    } 
    return result; 
} 

bunu biliyorum:

//check first in which camera we are 
public boolean isFrontCamera(int cameraIndex) { 
    try { 
     if (mDeviceManager != null) { 
      CameraCharacteristics characteristics = mDeviceManager.getCameraManager().getCameraCharacteristics(String.valueOf(cameraIndex)); 

      return characteristics.get(CameraCharacteristics.LENS_FACING) 
        == CameraCharacteristics.LENS_FACING_FRONT; 
     } 
    } catch (CameraAccessException e) { 
     e.printStackTrace(); 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } 

    return false; 
} 

    boolean mirror = isFrontCamera(Integer.parseInt(mCameraId)); 
       if (mirror) { 
        //Mirror image rotation is equal to the negative rotation of the device, but some external apps are not able to read negative numbers 
        //in image details, we do this to prevent the crash of this apps 
        rotation = 360 - rotation; 
        if (rotation == 360) { 
         rotation = 0; 
        } 
       } 

Daha sonra önce görüntü döndürmek görünümünüze eklemelisiniz, böyle bir Matrix kullanabilirsiniz Bitmap kullanmıyorsunuz, ancak yöntemi byteArray veya ihtiyacınız olan her şeye yollamak için yöntemi değiştirebilirsiniz.

Size biraz yardımcı olacağını umuyorum! Saygılarımızla!

İlgili konular