2015-04-26 78 views
0

SurfaceView上的攝像頭預覽被拉伸我試圖使相機預覽正確顯示在SurfaceView上。但是,儘管嘗試getSupportedPictureSizes()並將pictureSizepreviewSize設置爲列表中的其中一個值,但預覽仍然非常非常緊張。雖然設置大小

我決定我想要的大小通過720個像素是1280,我已經無處不在設置這個值(並再次,它是在getSupportedPictureSizes(),以及getSupportedPreviewSizes()

我已經瀏覽所有的#1的在這個問題上的帖子,我似乎無法解決。任何幫助表示讚賞。

surfaceCreated()我SurfaceHolder:

public void surfaceCreated(SurfaceHolder holder) 
{ 
    Camera.Parameters parameters = camera.getParameters(); 

    parameters.setPictureSize(1280, 720); 
    parameters.setPreviewSize(1280, 720); 

    Log.d("SurfaceView", surfaceView.getWidth() + ", " + surfaceView.getHeight()); 

    camera.setParameters(parameters); 
    camera.setDisplayOrientation(90); 

    previewCamera(); 
} 

SurfaceView設計XML:

<SurfaceView 
    android:layout_width="1280px" 
    android:layout_height="720px" 
    android:layout_gravity="center_horizontal" 
    android:id="@+id/surfaceViewCamera" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"/> 

整個活動類:

package org.ownzone.security_droid; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.hardware.Camera; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.WindowManager; 
import android.widget.CompoundButton; 
import android.widget.Switch; 
import android.widget.TextView; 

public class AuthenticatedActivity extends ActionBarActivity 
{ 
    private ServerConnection sc = ServerConnection.getServerConnectionInstance(); 
    private boolean active; 

    private Camera camera; 
    private SurfaceView surfaceView; 
    private SurfaceHolder surfaceHolder; 
    private boolean isPreviewRunning; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_authenticated); 

     active = false; 
     isPreviewRunning = false; 

     TextView headline = (TextView) findViewById(R.id.textViewHeadLine); 
     headline.setText(getIntent().getStringExtra("cardholder") + " authenticated"); 

     Switch activeSwitch = (Switch) findViewById(R.id.switchActive); 
     activeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
      { 
       active = isChecked; 
      } 
     }); 

     surfaceView = (SurfaceView) findViewById(R.id.surfaceViewCamera); 

     camera = Camera.open(); 

     for (Camera.Size size : camera.getParameters().getSupportedPictureSizes()) 
     { 
      Log.d("Size", size.width + ", " + size.height); 
     } 

     surfaceHolder = surfaceView.getHolder(); 
     surfaceHolder.addCallback(surfaceHolderCallback); 
     //camera.setPreviewDisplay(surfaceView.getHolder()); 
     //camera.startPreview(); 

     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    } 

    private void takePhoto() 
    { 
     camera.takePicture(shutterCallback, rawPictureCallback, jpegPictureCallback); 
    } 

    SurfaceHolder.Callback surfaceHolderCallback = new SurfaceHolder.Callback() 
    { 
     @Override 
     public void surfaceCreated(SurfaceHolder holder) 
     { 
      Camera.Parameters parameters = camera.getParameters(); 

      parameters.setPictureSize(1280, 720); 
      parameters.setPreviewSize(1280, 720); 

      Log.d("SurfaceView", surfaceView.getWidth() + ", " + surfaceView.getHeight()); 

      camera.setParameters(parameters); 
      camera.setDisplayOrientation(90); 

      previewCamera(); 
     } 

     @Override 
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
     { 

     } 

     @Override 
     public void surfaceDestroyed(SurfaceHolder holder) 
     { 

     } 
    }; 

    public void previewCamera() 
    { 
     try 
     { 
      camera.setPreviewDisplay(surfaceHolder); 
      camera.startPreview(); 
      isPreviewRunning = true; 
     } 
     catch (Exception e) 
     { 
      Log.d("AuthenticatedActivity", "Cannot start preview", e); 
     } 
    } 


    Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() 
    { 
     public void onShutter() 
     { 

     } 
    }; 

    Camera.PictureCallback rawPictureCallback = new Camera.PictureCallback() 
    { 
     public void onPictureTaken(byte[] data, Camera camera) 
     { 

     } 
    }; 

    Camera.PictureCallback jpegPictureCallback = new Camera.PictureCallback() 
    { 
     public void onPictureTaken(byte[] data, Camera camera) 
     { 
      Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data, 0, data.length); 
      Bitmap correctBmp = Bitmap.createBitmap(bitmapPicture, 0, 0, bitmapPicture.getWidth(), bitmapPicture.getHeight(), null, true); 

     } 
    }; 
} 
+0

這是哪種設備?你是陛下它支持1280x720預覽嗎?什麼是拉伸?捕獲的靜態JPEG圖片,或者在屏幕上實時預覽,或者在ImageView上渲染的位圖?如果您可以發佈帶有示例JPEG和屏幕截圖的更新,則會更容易。 –

回答

0

沒有截圖,這是一個猜測,但我會懷疑你的電話setDisplayOrientation。

硬編碼的值有可能適用於單個設備或一組設備,但在一般情況下,你需要考慮以下幾個因素 - 在這裏看到的文檔:http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int)

而且在一般情況下,大多數設備上都(但不是全部!),如果您在手機上安裝了橫向應用程序,則最終將調用setDisplayOrientation(0)而不是90.

您的應用是面向橫幅還是縱向(或靈活)? ?截圖將有助於排除各種問題。

相關問題