本人使用Camera显示预览图竖屏显示会有一些拉伸。感觉被拉长了。
本人已经选择了最接近的比例了,但是还是有拉伸。
本人已经选择了最接近的比例了,但是还是有拉伸。
package com.example.zxing_android; import java.io.IOException; import java.util.List; import com.example.zxing_android.R.color; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.Size; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout.LayoutParams; /** */ public class CameraActivity extends Activity { SurfaceView mSurface; SurfaceHolder mSurfaceHolder; Camera mCamera; boolean isInit; Button btn; // int viewWidth, viewHeight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); mSurface = (SurfaceView) findViewById(R.id.camera); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mCamera.autoFocus(null);// 自动对焦 } }); mSurfaceHolder = mSurface.getHolder(); // get the camera holder mSurfaceHolder.addCallback(new Callback() { @Override public void surfaceCreated(SurfaceHolder arg0) { mCamera = Camera.open(); // open camera isInit = false; try { mCamera.setPreviewDisplay(mSurfaceHolder); // set the // } catch (IOException e) { e.printStackTrace(); if (mCamera != null) { mCamera.release(); mCamera = null; } } mCamera.startPreview(); // state preview } @Override public void surfaceDestroyed(SurfaceHolder arg0) { // mSurface.setLayoutParams(new LayoutParams(viewWidth, // viewHeight)); mCamera.setPreviewCallback(null); // !这个必须在前,不然退出出错 mCamera.stopPreview(); mCamera.release(); mCamera = null; } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { initCamera(width, height); } }); } private void initCamera(int width, int height) { if (!isInit) { // viewWidth = width; // viewHeight = height; isInit = true; mCamera.autoFocus(null); Camera.Parameters parameters = mCamera.getParameters(); // 摄像头旋转 // if (android.os.Build.VERSION_CODES.GINGERBREAD > // android.os.Build.VERSION.SDK_INT) { mCamera.setDisplayOrientation(90); // } else { // parameters.set("rotation", 180); // } List<Size> previewSizes = mCamera.getParameters().getSupportedPreviewSizes(); double aspectTolerance = Double.MAX_VALUE; int preWidth = 0, preHeight = 0; double scale = ((double) width) / height; for (int i = 0, len = previewSizes.size(); i < len; i++) { Size s = previewSizes.get(i); double sizeScale = ((double) s.height) / s.width; if (Math.abs(scale - sizeScale) < aspectTolerance) { aspectTolerance = Math.abs(scale - sizeScale); preWidth = s.height; preHeight = s.width; } } if (preWidth != 0) { parameters.setPreviewSize(preWidth, preHeight); // mSurface.setLayoutParams(new LayoutParams(720, 1280)); Size s = parameters.getPreviewSize(); Log.e("", s.width + " " + s.height); } } } }
解决方案
10
输出下getSupportedPreviewSizes的全部值。例如1080*720
假如你竖屏是用整个手机屏幕显示预览,那么手机的长宽比例和预览返回的相差太多了。拉伸应该是不可避免。
不想拉伸的话就缩小屏幕显示的范围吧。
假如你竖屏是用整个手机屏幕显示预览,那么手机的长宽比例和预览返回的相差太多了。拉伸应该是不可避免。
不想拉伸的话就缩小屏幕显示的范围吧。
90