我想在SurfaceView中設置攝像頭預覽 當我在Surface中設置攝像頭時,它看起來像拉伸預覽。 我該如何解決這個問題? 試圖在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;`
`}`
`}`
你可以把你的代碼? – 2014-11-22 09:10:44
您的Surfaceview尺寸與視頻尺寸不一樣。或者根據寬度按比例減小高度,或者使用相機按鈕將寬度設置爲全屏幕作爲覆蓋層的頂部。 – 2014-11-22 09:19:13
我如何設置相同的尺寸? @Shobhit Puri – 2014-11-22 10:41:28