Android Studio 上 SurfaceView 的拉伸相机预览
Stretched camera preview on SurfaceView on Android Studio
这是我缩小 SurfaceView 时得到的结果。如何解决此问题以使其显示正确的纵横比?我不想使用从相机获得的整个图像,只使用其中的一部分。
把它想象成 Facebook Messenger 应用程序,当您想从该应用程序拍摄即时照片时,会出现一个小相机 window。我想要相同的结果。这是我的代码:
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
import java.util.List;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
public static final String TAG = "AKSQS";
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//Take care of rotation and resizing here.
mCamera.stopPreview();
mCamera.setDisplayOrientation(90);
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
for (Camera.Size sizesl : sizes) {
//This is just to see the supported preview sizes.
Log.d(TAG, "Width: " + String.valueOf(sizesl.width));
Log.d(TAG, "Height: " + String.valueOf(sizesl.height));
Log.d(TAG, "--------");
}
// parameters.setPreviewSize(1080,288);
mCamera.setParameters(parameters);
mCamera.startPreview();
if (mHolder.getSurface() == null) {
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
Log.d(TAG, "Tried to stop a non-existing preview. Message: " + e.getMessage());
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d(TAG, "Error starting camra preview: " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
这是我的 xml 文件:
<RelativeLayout ...>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="250dp"
android:id="@+id/camera_preview"
android:layout_alignParentBottom="true">
</FrameLayout>
</RelativeLayout>
这是我得到的结果:
这个问题困扰了我一个月。我终于在这里找到了答案:
这是最终结果:
这是我缩小 SurfaceView 时得到的结果。如何解决此问题以使其显示正确的纵横比?我不想使用从相机获得的整个图像,只使用其中的一部分。
把它想象成 Facebook Messenger 应用程序,当您想从该应用程序拍摄即时照片时,会出现一个小相机 window。我想要相同的结果。这是我的代码:
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
import java.util.List;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
public static final String TAG = "AKSQS";
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//Take care of rotation and resizing here.
mCamera.stopPreview();
mCamera.setDisplayOrientation(90);
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
for (Camera.Size sizesl : sizes) {
//This is just to see the supported preview sizes.
Log.d(TAG, "Width: " + String.valueOf(sizesl.width));
Log.d(TAG, "Height: " + String.valueOf(sizesl.height));
Log.d(TAG, "--------");
}
// parameters.setPreviewSize(1080,288);
mCamera.setParameters(parameters);
mCamera.startPreview();
if (mHolder.getSurface() == null) {
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
Log.d(TAG, "Tried to stop a non-existing preview. Message: " + e.getMessage());
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d(TAG, "Error starting camra preview: " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
这是我的 xml 文件:
<RelativeLayout ...>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="250dp"
android:id="@+id/camera_preview"
android:layout_alignParentBottom="true">
</FrameLayout>
</RelativeLayout>
这是我得到的结果:
这个问题困扰了我一个月。我终于在这里找到了答案:
这是最终结果: