2011-11-28 66 views
4

下面的代碼顯示垂直相機的預覽,它的作品。 但是!!我在風景中得到一張照片! :(安卓相機問題(垂直相機|人像)

如何我可以垂直構建呢? 我在垂直預覽視圖,但我無法保存圖片垂直。

的問候和感謝, 弗蘭

ONCLICK

public void onClick(View arg0) { 
     camera.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG); 
} 

PREVIEW

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
    if (previewing) { 
     camera.stopPreview(); 
     previewing = false; 
    } 
    if (camera != null) { 
     /* 
     * camera.setPreviewDisplay(surfaceHolder); camera.startPreview(); 
     * previewing = true; 
     */ 
     Camera.Parameters parameters = camera.getParameters(); 
     parameters.setPreviewSize(width, height); 
     camera.setParameters(parameters); 

     // Condicions per utilitzar la orientacio adecuada segons el sdk 
     if (Integer.parseInt(Build.VERSION.SDK) >= 8) 
      setDisplayOrientation(camera, 90); 
     else { 
      if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
       parameters.set("orientation", "portrait"); 
       parameters.set("rotation", 90); 
      } 
      if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
       parameters.set("orientation", "landscape"); 
       parameters.set("rotation", 90); 
      } 
      camera.setParameters(parameters); 
     } 

     // camera.startPreview(); 
     previewing = true; 
    } 
} 

拍照

PictureCallback myPictureCallback_JPG = new PictureCallback() { 
    @Override 
    public void onPictureTaken(byte[] arg0, Camera arg1) { 
     // intentar canvia horientacio 
     Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length); 
     //bitmapPicture. 

     OutputStream imageFileOS; 
     try { 
      imageFileOS = new FileOutputStream(String.format("/sdcard/DCIM/iboo/captura.jpg")); 
      imageFileOS.write(arg0); 
      imageFileOS.flush(); 
      imageFileOS.close(); 
      // Missatge en cas de capturar l'imatge correctament 
      Toast.makeText(Principal.this, "Imatge Capturada!", 
        Toast.LENGTH_LONG).show(); 
      MostrarFoto("/sdcard/DCIM/iboo/captura.jpg"); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // camera.startPreview(); 


       } 
}; 

回答

7

這是我實施的解決方案。它完美的作品。我希望有所幫助。

PictureCallback myPictureCallback_JPG = new PictureCallback() { 
    @Override 
    public void onPictureTaken(byte[] arg0, Camera arg1) { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 6; 
     options.inDither = false; // Disable Dithering mode 
     options.inPurgeable = true; // Tell to gc that whether it needs free 
            // memory, the Bitmap can be cleared 
     options.inInputShareable = true; // Which kind of reference will be 
              // used to recover the Bitmap 
              // data after being clear, when 
              // it will be used in the future 
     options.inTempStorage = new byte[32 * 1024]; 
     options.inPreferredConfig = Bitmap.Config.RGB_565; 
     bMap = BitmapFactory.decodeByteArray(arg0, 0, arg0.length, options); 

     // others devices 
     if(bMap.getHeight() < bMap.getWidth()){ 
      orientation = 90; 
     } else { 
      orientation = 0; 
     } 

     Bitmap bMapRotate; 
     if (orientation != 0) { 
      Matrix matrix = new Matrix(); 
      matrix.postRotate(orientation); 
      bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), 
        bMap.getHeight(), matrix, true); 
     } else 
      bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(), 
        bMap.getHeight(), true); 


     FileOutputStream out; 
     try { 
      out = new FileOutputStream(
        String.format("/sdcard/DCIM/test/screen.jpg")); 
      bMapRotate.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      if (bMapRotate != null) { 
       bMapRotate.recycle(); 
       bMapRotate = null; 
      } 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     camera.startPreview(); 
     MostrarFoto(imageInSD); 
     editor = prefs.edit(); 
     editor.putString("PathSeleccio", imageInSD); 
     editor.commit(); 
    } 
}; 
+0

我不知道你在哪裏得到的bMap變量和方向變量似乎並沒有宣佈。 – jimbob

2

看一看here

+0

謝謝回答。我是新來的Java,我還沒有實現它。誰能幫我? – ephramd

+0

此代碼僅適用於API LEVEL 8(版本2.2),我使用版本2.1(API級別7)。我不知道我能做什麼。¿任何想法? – ephramd

+0

哪些行不能編譯? –

0

對於設置下面的代碼的圖像使用的方向:

Matrix mat = new Matrix(); 
mat.postRotate(90); 
image_to_upload = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(), myImage.getHeight(), mat, true); 
0

你實際上並不需要旋轉位圖。這是消耗內存/慢等...看起來它是在你的情況更好(節省拍攝後立即到文件)來更新Exif tags in a JPEG file,例如:

int degrees = 90; 
    ExifInterface exif = new ExifInterface(path); 
    exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(degrees)); 
    exif.saveAttributes(); 
+0

對我不起作用。我通過了這個路徑,但圖像仍處於橫向模式,/ storage/sdcard0/images/Hijab2Go/IMG_20140318_114648.jpg – Hesam

1
Camera.Parameters param; 
param = camera.getParameters(); 

Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 
if(display.getRotation() == Surface.ROTATION_0) 
{ 
    camera.setDisplayOrientation(90); 
    param.setRotation(90); 
} 
if(display.getRotation() == Surface.ROTATION_270) 
{ 
    camera.setDisplayOrientation(180); 
    param.setRotation(180); 
} 

camera.setParameters(param); 
camera.takePicture(shutterCallback, rawCallback, jpegCallback);