1

我想在SurfaceView中設置攝像頭預覽 當我在Surface中設置攝像頭時,它看起來像拉伸預覽。 我該如何解決這個問題? enter image description here試圖在SurfaceView中設置時拉伸攝像頭預覽

`public class CamActivity extends Activity implements SurfaceHolder.Callback` 
`{` 
`Camera camera;` 
`SurfaceView surface;` 
`SurfaceHolder mholder;` 
`Button capture;` 
`Bitmap bitmap;` 
`public String path = Environment.getDataDirectory().getAbsolutePath() + "/storage/emulated/0/Pictures/Cam";` 
@Override 
`protected void onCreate(Bundle savedInstanceState) ` 
`{` 
` super.onCreate(savedInstanceState);` 
    ` setContentView(R.layout.activity_cam);` 
    ` surface=(SurfaceView)findViewById(R.id.camera_view);` 
    ` if(mholder==null)` 
    ` mholder=surface.getHolder();` 
    ` mholder.addCallback(this);` 
    `mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);` 

    `capture=(Button)findViewById(R.id.camera_capture);` 
    `File mFolder = new File(path);` 
    `if (!mFolder.exists()) {` 
    ` mFolder.mkdir();` 
    `}` 
    `capture.setOnClickListener(new OnClickListener() {` 

    ` @SuppressWarnings("deprecation")` 
    ` @Override` 
    ` public void onClick(View v) {` 
    `  camera.takePicture(null, null, new PictureCallback()` 
    `  {` 

       @Override 
    `   public void onPictureTaken(byte[] data, Camera camera)` 
    `   {` 

    `    Random generator = new Random();` 
    `    int n = 10000;` 
    `    n = generator.nextInt(n);` 
    `    String fname = "Image-"+ n +".jpg";` 
    `    File pictureFile = new File(Environment.getExternalStoragePublicDirectory(` 
    `       Environment.DIRECTORY_PICTURES)+"/", fname);` 
    `    try {` 
    `      FileOutputStream fos = new FileOutputStream(pictureFile);` 
    `      bitmap.compress(Bitmap.CompressFormat.JPEG,90, fos);` 
    `      fos.flush();` 
    `      fos.close();` 
    `     } catch (FileNotFoundException e) {` 
          `Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();` 
    `     } catch (IOException e) {` 
          `Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();` 
    `     }` 

    `   }` 
    `  });` 
    ` }` 
    `});` 
`}` 

@Override 
`public void surfaceCreated(SurfaceHolder holder) ` 
`{` 
` camera=Camera.open();` 
` try` 
` {` 
`  camera.setPreviewDisplay(holder);` 
`  Toast.makeText(getApplicationContext(), path, Toast.LENGTH_LONG).show();` 
` } ` 
` catch (IOException exception)` 
` {` 
`   camera.release();` 
`   camera = null;` 
` }` 
`}` 

@Override 
`public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) ` 
`{` 
` camera.startPreview();` 
    ` camera.setDisplayOrientation(90);` 
`}` 

@Override 
`public void surfaceDestroyed(SurfaceHolder holder)` 
`{ 
` camera.stopPreview();` 
` camera.release();` 
` camera = null;`   
`}` 
`}` 
+2

你可以把你的代碼? – 2014-11-22 09:10:44

+1

您的Surfaceview尺寸與視頻尺寸不一樣。或者根據寬度按比例減小高度,或者使用相機按鈕將寬度設置爲全屏幕作爲覆蓋層的頂部。 – 2014-11-22 09:19:13

+0

我如何設置相同的尺寸? @Shobhit Puri – 2014-11-22 10:41:28

回答

2

你要聽的活動方向變化,並設置適當的方位相機。

此方法添加到您的相機活動:

public void setCameraDisplayOrientation(Activity activity) { 

    if(null == mCamera){ 
     return; 
    } 

     android.hardware.Camera.CameraInfo info = 
      new android.hardware.Camera.CameraInfo(); 

     android.hardware.Camera.getCameraInfo(cameraId, info); 

     int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 
     int degrees = 0; 

     switch (rotation) { 
      case Surface.ROTATION_0: degrees = 0; break; 
      case Surface.ROTATION_90: degrees = 90; break; 
      case Surface.ROTATION_180: degrees = 180; break; 
      case Surface.ROTATION_270: degrees = 270; break; 
     } 


     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
      orientation = (info.orientation + degrees) % 360; 
      orientation = (360 - orientation) % 360; // compensate the mirror 
     } else { // back-facing 
      orientation = (info.orientation - degrees + 360) % 360; 
     } 

     if(null != mCamera){ 
      mCamera.setDisplayOrientation(orientation); 
     } 
    } 

還添加OrientationEventListner

mOrientationEventListener = new OrientationEventListener(mApplication, 
      SensorManager.SENSOR_DELAY_NORMAL) { 

     @Override 
     public void onOrientationChanged(int orientation) { 

      if ((orientation == ORIENTATION_UNKNOWN) || (mCamera == null)) { 
       return; 
      } 

      Camera.Parameters params    = mCamera.getParameters();    
      android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); 

      android.hardware.Camera.getCameraInfo(cameraId, info); 

      orientation = (orientation + 45)/90 * 90; 

      int rotation = 0; 

      if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
       rotation = (info.orientation - orientation + 360) % 360; 
      } 
      else { 
       /* 
       * back-facing camera 
       */ 
       rotation = (info.orientation + orientation) % 360; 
      } 

      params.setRotation(rotation); 

      if(null == mCamera) { 
       return; 
      } 

      mCamera.setParameters(params); 
     } 

    }; 

啓用定向聽衆一旦活動開始 /* *開始向聽衆 */
如果(mOrientationEventListener.canDetectOrientation()){
mOrienta tionEventListener.enable(); }

,並在活動的onConfigurationChanged和的onResume回調,進行下面的調用

setCameraDisplayOrientation(活動性)

希望這有助於

問候, Shrish

編輯更新: 請查看這個示例代碼的相機,你的疑惑大部分應該得到清除 https://github.com/shrishmv/CameraTest

+0

我怎樣才能得到cameraId? – 2014-11-24 06:05:15

+0

它不適合我...你能給我完整的代碼嗎? – 2014-11-24 06:41:03

+0

請給我回答 – 2014-11-24 09:35:11

相關問題