我正在尝试在我的应用程序上实现相机功能。由于这是我第一次这样做,我在相机预览中遇到了一些问题。
我目前的布局如下:
正在制作这样的预览:
但我想要这样的事情:
以下是我相机的代码:
public class FragmentPhoto extends Fragment implements TextureView.SurfaceTextureListener {
private Camera camera;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_photo, container, false);
final TextureView textureView = (TextureView) view.findViewById(R.id.photo_view);
textureView.setSurfaceTextureListener(this);
return view;
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
try {
camera = Camera.open();
camera.setDisplayOrientation(90);
camera.setPreviewTexture(surface);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
camera.stopPreview();
camera.release();
return true;
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// Ignored, the Camera does all the work for us
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Update your view here!
}
}
有没有办法裁剪相机?为了不预览这个胖乎乎的?这样做最好的方法是什么?