我正在制作一个自定义相机应用程序,它将拍照并将其保存在图库中。 我有两个问题: 1:我无法访问前置摄像头。我添加了另一个按钮并添加了前置摄像头代码,但它不起作用。所以有人可以告诉我应该做什么才能切换到前置摄像头。
2:我的第二个问题是当我的图像被保存时,它的方向变为横向。
here is my code :
public class MainActivity extends AppCompatActivity {
public final static String DEBUG_TAG = "MainActivity";
private Camera mCamera = null;
private Camera.PreviewCallback previewCb;
private Camera.AutoFocusCallback autoFocusCb;
private CameraView mCameraView = null;
public static final int MEDIA_TYPE_IMAGE = 0;
public static int i = 0;
private static final int CAMERA_REQUEST = 1888;
public final static int PICK_PHOTO_CODE = 1046;
ImageButton button;
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView mImageView;
private int cameraId = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mImageView = (ImageView) findViewById(R.id.imageView);
try {
mCamera = Camera.open();
} catch (Exception e) {
Log.d("ERROR", "Failed to get camera: " + e.getMessage());
}
if (mCamera != null) {
mCameraView = new CameraView(this, mCamera, previewCb,autoFocusCb);
FrameLayout camera_view = (FrameLayout) findViewById(R.id.camera_view);
camera_view.addView(mCameraView);//add the SurfaceView to the layout
}
}
public void OnBtnClick(View view) {
mCamera.autoFocus(new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera)
{
mCamera.startPreview();
mCamera.takePicture(null, null, new PhotoHandler(getApplicationContext()));
}
});
}
public void OnBtn1Click(View view) {
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
mImageView.setImageBitmap(photo);
}
}
}
我的相机预览课程
public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
public final static String DEBUG_TAG = "MainActivity";
private SurfaceHolder mHolder;
private Camera.AutoFocusCallback autoFocusCallback;
private int cameraId = 0;
private Camera mCamera;
int currentZoomLevel = 0;
private Camera.PreviewCallback previewCallback;
public CameraView(Context context, Camera camera, Camera.PreviewCallback previewCb,Camera.AutoFocusCallback autoFocusCb){
super(context);
previewCallback = previewCb;
autoFocusCallback = autoFocusCb;
mCamera = camera;
mCamera.setDisplayOrientation(90);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try{
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("ERROR", "Camera error on surfaceCreated " + e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
if(mHolder.getSurface() == null)
return;
try{
mCamera.stopPreview();
} catch (Exception e){
}
try{
mCamera.setDisplayOrientation(90);
mCamera.setPreviewCallback(previewCallback);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
mCamera.autoFocus(autoFocusCallback);
mCamera.autoFocus(autoFocusCallback);
} catch (IOException e) {
Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
mCamera.stopPreview();
mCamera.release();
}
}
和我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:layout_gravity="center_horizontal" />
</FrameLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:onClick="OnBtnClick"
android:layout_gravity="center_horizontal|bottom"
android:background = "@drawable/roundedbutton"
android:src="@drawable/btn_capture" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgClose"
android:layout_gravity="right|top"
android:background="@android:drawable/ic_menu_close_clear_cancel"
android:padding="20dp"/>
<ImageButton
android:layout_width="113dp"
android:layout_height="94dp"
android:id="@+id/imageButton"
android:layout_gravity="left|bottom"
android:background = "@drawable/roundedbutton"
android:src="@drawable/front"
android:onClick="OnBtn1Click"/>
</FrameLayout>