gpt4 book ai didi

android - 自定义相机以错误的方向显示预览和缩放图像android

转载 作者:行者123 更新时间:2023-11-30 01:45:36 26 4
gpt4 key购买 nike

我正在使用简单的自定义相机,它会在点击按钮时拍照。

相机以错误的方向(横向)显示预览,即使手机处于纵向,图像也出现一些缩放。

用谷歌搜索它并找到了许多解决这个问题的方法,我尝试了所有方法,但没有人适用于我的情况。

我想我遗漏了一些我无法理解的主要概念

enter image description here

CameraActivity.java

public class CameraActivity extends AppCompatActivity {
private static final String TAG = "CameraActivity";
private Camera mCamera;
private CameraPreview mPreview;
private PictureCallback mPictureCallback;
private Context myContext;
private FrameLayout cameraPreview;
private boolean frontCamera = false, backCamera = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
initialize();
}

private void findViews() {
cameraPreview = (FrameLayout) findViewById(R.id.camera_preview);
}

public void initialize() {
findViews();
myContext = this;
mCamera = Camera.open(findBackFacingCamera());
mPreview = new CameraPreview(myContext, mCamera);
cameraPreview.addView(mPreview);
}

private int findFrontFacingCamera() {
int cameraId = -1; // initialized with empty
int numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
cameraId = i;
frontCamera = true;
break;
}
}
return cameraId;
}

private int findBackFacingCamera() {
int cameraId = -1; // initialized with empty
int numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
cameraId = i;
backCamera = true;
break;
}
}
return cameraId;
}

@Override
protected void onResume() {
super.onResume();
//mPictureCallback=getPi
if (!hasCamera(myContext)) {
Log.e(TAG, "Sorry, your phone does not have a camera!");
}
if (mCamera == null) {
if (findFrontFacingCamera() == 1) {
releaseCamera();
chooseCamera();
}
}
}

private void chooseCamera() {
// If currently front camera is opened,then switch to back camera
if (frontCamera) {
int cameraId = findBackFacingCamera();
if (cameraId > 0) {
// Open backfacing camera
// Set picture callback
// refresh the preview
mCamera = Camera.open(cameraId);
mPictureCallback = getPictureCallBack();
mPreview.refreshCamera(mCamera);
}
} else {
int cameraId = findFrontFacingCamera();
if (cameraId > 0) {
mCamera = Camera.open(cameraId);
mPictureCallback = getPictureCallBack();
mPreview.refreshCamera(mCamera);
}
}
}

private PictureCallback getPictureCallBack() {
PictureCallback pictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {


File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Log.e(TAG, " picture saved at " + pictureFile.getName());
} catch (Exception e) {
Log.e(TAG, " getPictureCallBack " + e);
}
mPreview.refreshCamera(mCamera);
}
};
return pictureCallback;
}

private File getOutputMediaFile() {
// make a new storage directory inside sd card
File mediaStorage = new File("/sdcard/", "Tootle Camera");

// if Tootle Camera directory doesn't exist
if (!mediaStorage.exists()) {
if (!mediaStorage.mkdirs()) {
return null;
} else {
mediaStorage.mkdirs();
}
}

String timeStamp = new SimpleDateFormat("dd/MM/yyy_hh:mm:ss a").format(new Date());

// make media file
File mediaFile = new File(mediaStorage.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
return mediaFile;
}

private void releaseCamera() {
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}

public boolean hasCamera(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
} else {
return false;
}
}

@Override
protected void onPause() {
super.onPause();
releaseCamera();
}
}

CameraPreview.java

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private static final String TAG = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera;
private boolean isPreviewRunning = false;

public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
}


@Override
public void surfaceCreated(SurfaceHolder holder) {

try {
// create the surface and start camera previre
if (mCamera == null) {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();

}


} catch (Exception e) {
Log.e(TAG, " surfaceCreated " + e);
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
//refreshCamera(mCamera);

if (isPreviewRunning) {
mCamera.stopPreview();
}

Camera.Parameters parameters = mCamera.getParameters();
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

if (display.getRotation() == Surface.ROTATION_0) {
parameters.setPreviewSize(height, width);
mCamera.setDisplayOrientation(90);
}

if (display.getRotation() == Surface.ROTATION_90) {
parameters.setPreviewSize(width, height);
}

if (display.getRotation() == Surface.ROTATION_180) {
parameters.setPreviewSize(height, width);
}

if (display.getRotation() == Surface.ROTATION_270) {
parameters.setPreviewSize(width, height);
mCamera.setDisplayOrientation(180);
}

mCamera.setParameters(parameters);
previewCamera();

}

public void refreshCamera(Camera camera) {

if (mHolder == null) {
// preview surface does not exist
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
Log.e(TAG, " refreshCamera " + e);
}
setCamera(camera);
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.e(TAG, " refreshCamera " + e);
}
}

private void setCamera(Camera camera) {
//method to set a camera instance
mCamera = camera;
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.release();
}

public void previewCamera() {
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
isPreviewRunning = true;
} catch (Exception e) {
Log.d(TAG, "Cannot start preview " + e);
}
}
}

最佳答案

在您的CameraPreview.java

之后

setCamera(camera);
try {
mCamera.setPreviewDisplay(mHolder);

这样写:

mCamera.setDisplayOrientation(90);
mCamera.startPreview();

关于android - 自定义相机以错误的方向显示预览和缩放图像android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33774518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com