2

我正在尝试制作自拍应用程序。我捕获了图像并从前置摄像头保存了它。但在图库或文件管理器中的图像未打开。 “无法加载图像”这条消息来了。如果我将标志从前置摄像头更改为后置摄像头,则它可以正常工作。

public class Preview extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder mHolder;
    private Camera mCamera;
    private Context context;
    private String TAG = "Preview";

    public Preview(Camera mCamera, Context context) {
        super(context);

        this.mCamera = mCamera;
        this.context = context;

        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder mHolder) {
        safeCameraOpen();
        if (mCamera != null) {
            try {
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();

            } catch (IOException e) {
                Log.d(TAG, "Error setting camera preview: " + e.getMessage());
                mCamera.release();
                mCamera = null;
            }
        } else {
            Log.d(TAG, "Error in camera  : ");
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (mCamera != null) {
            setCameraDisplayOrientation((Activity) context, mCamera);
        }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        releaseCameraAndPreview();
    }

    // This method is called while clicking for selfie
    public void captureImage() {
        mCamera.takePicture(null, null, mPicture);
    }

    Camera.PictureCallback mPicture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
           saveImage(data);
        }
    };

    public void saveImage(byte[] data) {
        try {
            File imageFile = createImageFile();

            if (imageFile != null) {
                FileOutputStream fos = new FileOutputStream(imageFile);
                fos.write(data);
                fos.close();
                Toast.makeText(context, "New Image saved", Toast.LENGTH_LONG).show();

            } else {
                Toast.makeText(context, "Directory problem", Toast.LENGTH_LONG).show();

            }
        } catch (Exception error) {
            error.getMessage();
            Toast.makeText(context, "Image could not be saved.", Toast.LENGTH_LONG).show();
        }
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Selfie");
        if (!storageDir.exists() && !storageDir.mkdirs()) {

            Log.d(TAG, "Can't create directory to save image.");
            Toast.makeText(context, "Can't create directory to save image.", Toast.LENGTH_LONG).show();
            return null;

        }
        File image = File.createTempFile(String.valueOf(System.currentTimeMillis()),  /* prefix */ ".jpg", /* suffix */ storageDir /* directory */);

        return image;
    }


    private boolean safeCameraOpen() {
        boolean qOpened = false;

        try {
            releaseCameraAndPreview();
            mCamera = openFrontFacing();
            qOpened = (mCamera != null);
        } catch (Exception e) {

            e.printStackTrace();
        }

        return qOpened;
    }

    public void releaseCameraAndPreview() {
        //  mPreview.setCamera(null);
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }

    private Camera openFrontFacing() {
        int cameraCount = 0;
        Camera camera = null;

        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();
        for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                try {
                    camera = Camera.open(camIdx);
                } catch (RuntimeException e) {
                    Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
                }
            }
        }

        return camera;
    }
}

openFrontFacing()如果我改变它的方法CAMERA_FACING_FRONTCAMERA_FACING_BACK它工作得很好。

4

1 回答 1

0

每个相机都有一些支持的预览尺寸和图片尺寸,你必须设置正确的。它的主题有点长,搜索 mCamera.getParameters().getSupportedPictureSizes(); 和 mCamera.getParameters().getSupportedPreviewSizes();

于 2016-06-18T20:04:41.003 回答