gpt4 book ai didi

java - 用相机拍照但不保存为文件,仅显示

转载 作者:太空宇宙 更新时间:2023-11-04 15:12:39 26 4
gpt4 key购买 nike

在我的 Android 项目中,我想用相机拍照但不将其保存为文件,仅将其显示在 ImageView 上、可绘制或其他形式。

在我的项目中,我可以看到相机,如下面的代码。

我尝试了很多方法,但如果不保存SDCard,我就无法拍照。

ShowCamera.java

 public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder holdMe;
private Camera theCamera;

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

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
theCamera.setPreviewDisplay(holder);
theCamera.startPreview();
} catch (IOException e) {
}
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
}

MainActivity.java

public class MainActivity extends Activity  {

private Camera cameraObject;
private ShowCamera showCamera;

public static Camera isCameraAvailiable(){
Camera object = null;
try {
object = Camera.open();
}
catch (Exception e){
}
return object;
}



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);

cameraObject = isCameraAvailiable();
showCamera = new ShowCamera(this, cameraObject);
final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}

谢谢!

最佳答案

首先,不要忘记将其放入 AndroidManifest.xml 中:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

因此,您可以按照以下示例操作:http://hmkcode.com/android-camera-taking-photos-camera/

关于java - 用相机拍照但不保存为文件,仅显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21175542/

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