自定义相机新捕获的图像将覆盖旧的

时间:2019-06-12 05:21:26

标签: android camera-api

我创建了一个自定义相机,可以拍摄照片并将其存储在手机内部存储器中的文件夹中。但是,每当我拍摄一张新照片时,它就会覆盖旧照片。

MainActivity

public class MainActivity extends AppCompatActivity {
Camera camera;
Button capture, gallery;
FrameLayout frameLayout;
ShowCamera showCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    frameLayout = findViewById(R.id.frameLayout);
    capture = findViewById(R.id.button);
    gallery = findViewById(R.id.button2);

    camera = Camera.open();
    showCamera = new ShowCamera(this,camera);
    frameLayout.addView(showCamera);

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            captureImage(showCamera);
        }
    },4000);
}

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File picture_file = getOutputMediaFile();
            if(picture_file==null) {
                return;
            }
            try {
             FileOutputStream fos = new FileOutputStream(picture_file);
                fos.write(data);
                fos.close();
                camera.startPreview();
            }  catch (IOException e) {
                e.printStackTrace();
            }
        }

         };


private File getOutputMediaFile() {
    String state = Environment.getExternalStorageState();
    if(!state.equals(Environment.MEDIA_MOUNTED)) {
        return null;
    }
    else {
        File folder_gui = new File (Environment.getExternalStorageDirectory() + File.separator+"GUI");
        if(!folder_gui.exists()) {
            folder_gui.mkdirs();
        }
        File outputFile = new File(folder_gui,"temp.jpg");
        return outputFile;
    }
}

public void captureImage(View v) {
    if(camera!=null) {
        camera.takePicture(null,null,mPictureCallback);
    }
}
}

ShowCamera

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 surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

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

@Override
public void surfaceCreated(SurfaceHolder holder) {
    Camera.Parameters params = camera.getParameters();
    //change the orientation of the camera

    List<Camera.Size> sizes = params.getSupportedPictureSizes();
    Camera.Size mSize = null;
    for(Camera.Size size : sizes) {
        mSize = size;
    }

    if(this.getResources().getConfiguration().orientation!= Configuration.ORIENTATION_LANDSCAPE) {
        params.set("orientation","portrait");
        camera.setDisplayOrientation(90);
        params.setRotation(90);
    }
    else {
        params.set("orientation","landscape");
        camera.setDisplayOrientation(0);
        params.setRotation(0);
    }
    params.setPictureSize(mSize.width,mSize.height);

    camera.setParameters(params);
    try {
        camera.setPreviewDisplay(holder);
        camera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

当拍摄一张新照片时,我希望将该照片添加到文件夹中,而不是覆盖现有照片。我该怎么做?

1 个答案:

答案 0 :(得分:1)

以此替换代码

    private File getOutputMediaFile() {
    String state = Environment.getExternalStorageState();
    if(!state.equals(Environment.MEDIA_MOUNTED)) {
        return null;
    }
    else {
        File folder_gui = new File (Environment.getExternalStorageDirectory() + File.separator+"GUI");
        if(!folder_gui.exists()) {
            folder_gui.mkdirs();
        }
        File outputFile = new File(folder_gui,System.currentTimeInMillies()+."jpg");
        return outputFile;
    }
}

因为您每次都提供相同的名称,所以它将替换您的旧文件