相机API。不保存图像

时间:2018-03-06 14:40:48

标签: java android android-fragments camera fileoutputstream

我正在使用Camera API创建Camera应用程序1.我在片段中执行此操作。我有FrameLayout,我添加了相机,还有一个按钮用于捕捉。但拍照后相机不能保存照片。我不知道问题出在哪里,我第一次使用相机API。你能帮我找到解决方案吗?

其他问题对我没有帮助。他们的代码不起作用。

我只在纵向模式下使用的相机。它表明,我添加了相机的权限和写入外部存储。

我有一个类,我正在扩展SurfaceView。让我告诉你代码。

    public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback{

Camera camera;
SurfaceHolder holder;

public ShowCamera(Context context, Camera camera) {
    super(context);
    this.camera = camera;
    holder = getHolder();
    holder.addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    Camera.Parameters params = camera.getParameters();

    List<Camera.Size> sizes = params.getSupportedPictureSizes();
    Camera.Size mSize = null;

    for (Camera.Size size : sizes){
        mSize = size;
    }

    camera.setDisplayOrientation(90);
    params.setRotation(90);

    params.setPictureSize(mSize.width, mSize.height);
    camera.setParameters(params);
    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    camera.stopPreview();
    camera.release();
}
}

好的,这是相机的片段。

    public class CameraActivity extends BaseFragment{
View mainView;
FrameLayout cameraLayout;
Camera camera;
ShowCamera showCamera;
ImageButton captureImage;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.camera_fragment, container, false);

    cameraLayout = (FrameLayout) mainView.findViewById(R.id.cameraContainer);
    captureImage = (ImageButton) mainView.findViewById(R.id.imageButton);

    camera = Camera.open();
    showCamera = new ShowCamera(getActivity(), camera);

    cameraLayout.addView(showCamera);

    capture();

    return mainView;
}

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] bytes, Camera camera) {
        if(bytes != null){
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            if(bitmap != null){
                File file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)));
                if(!file.isDirectory()){
                    file.mkdir();
                }

                file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
                        System.currentTimeMillis() + ".jpg");

                try {
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);

                    fileOutputStream.flush();
                    fileOutputStream.close();

                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
};

public void capture(){
    captureImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(camera != null){
                camera.takePicture(null, null, mPictureCallback);
            }
        }
    });

}
}

帮我找到解决方案。谢谢你的阅读。

0 个答案:

没有答案