gpt4 book ai didi

java - Android OpenCV - CameraBridgeViewBase 拍照?

转载 作者:太空宇宙 更新时间:2023-11-03 10:19:39 25 4
gpt4 key购买 nike

拜托,有人可以帮我解决这个问题吗?
我有一个使用 CameraBridgeViewBase 的项目,我想拍照并保存文件,但是 CameraBridgeViewBase 没有实现 takePicture 函数。

最佳答案

结果:我实现了一个新类扩展 JavaCameraView 并自己实现了函数 takePicture。

import org.opencv.android.JavaCameraView;

import java.io.FileOutputStream;
import java.util.List;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.util.AttributeSet;
import android.util.Log;

public class MyCameraView extends JavaCameraView implements PictureCallback {

private static final String TAG = "myCameraView";
private String mPictureFileName;

public MyCameraView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public List<String> getEffectList() {
return mCamera.getParameters().getSupportedColorEffects();
}

public boolean isEffectSupported() {
return (mCamera.getParameters().getColorEffect() != null);
}

public String getEffect() {
return mCamera.getParameters().getColorEffect();
}

public void setEffect(String effect) {
Camera.Parameters params = mCamera.getParameters();
params.setColorEffect(effect);
mCamera.setParameters(params);
}

public List<Size> getResolutionList() {
return mCamera.getParameters().getSupportedPreviewSizes();
}

public void setResolution(Size resolution) {
disconnectCamera();
mMaxHeight = resolution.height;
mMaxWidth = resolution.width;
connectCamera(getWidth(), getHeight());
}

public Size getResolution() {
return mCamera.getParameters().getPreviewSize();
}

public void takePicture(final String fileName) {
Log.i(TAG, "Taking picture");
this.mPictureFileName = fileName;
// Postview and jpeg are sent in the same buffers if the queue is not empty when performing a capture.
// Clear up buffers to avoid mCamera.takePicture to be stuck because of a memory issue
mCamera.setPreviewCallback(null);

// PictureCallback is implemented by the current class
mCamera.takePicture(null, null, this);
}

@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.i(TAG, "Saving a bitmap to file");
// The camera preview was automatically stopped. Start it again.
mCamera.startPreview();
mCamera.setPreviewCallback(this);

// Write the image in a file (in jpeg format)
try {
FileOutputStream fos = new FileOutputStream(mPictureFileName);

fos.write(data);
fos.close();

} catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}

}
}

-

    private MyCameraView mOpenCvCameraView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

mOpenCvCameraView = (MyCameraView) view.findViewById(R.id.fd_activity_surface_view);
mOpenCvCameraView.setCvCameraViewListener(this);

return view;
}

@Override
public void onResume() {
super.onResume();
mOpenCvCameraView.enableView();

String filename = "teste.jpg";
mOpenCvCameraView.takePicture(filename);
mOpenCvCameraView.disableView();
}

关于java - Android OpenCV - CameraBridgeViewBase 拍照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26187378/

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