gpt4 book ai didi

java - Android 相机预览未显示

转载 作者:行者123 更新时间:2023-12-01 21:18:18 25 4
gpt4 key购买 nike

我在网上找到了教程here两者都用于捕获图像,它们非常相似,我使用另一个来找出为什么我的相机代码不起作用。

我在 Android 中没有遇到任何语法错误,但是当我进入所需的 fragment 时,它只是一个白色屏幕,没有相机显示,我不知道为什么我深入查看了这两个代码示例并用谷歌搜索了我的代码有问题但找不到任何东西。与我的代码的唯一区别是它是一个 fragment 而不是一个 Activity 。有人可以帮我吗?

这是我的代码:

public class Image extends Fragment implements SurfaceHolder.Callback {

private ImageView imageView;
private SurfaceView mSurfaceView;
private Bitmap capturedImage;


//Camera

private SurfaceHolder sHolder;
private Camera mCamera;
private Parameters parameters;

/**********************************************/

public Image() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

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

imageView = (ImageView) view.findViewById(R.id.imageView);
mSurfaceView = (SurfaceView) view.findViewById(R.id.surfaceView);

//Get a surface
sHolder = mSurfaceView.getHolder();

//add the callback interface methods defined below as the Surface View callbacks
sHolder.addCallback(this);

//tells Android that this surface will have its data constantly replaced
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

return view;
}

@Override
public void surfaceCreated(SurfaceHolder holder) {

// The Surface has been created, acquire the camera and tell it where
// to draw the preview.

mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);

} catch (IOException exception) {
mCamera.release();
mCamera = null;
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {


//get camera parameters
parameters = mCamera.getParameters();

parameters.setPreviewSize(352, 288);
//set camera parameters
mCamera.setParameters(parameters);
mCamera.startPreview();

//sets what code should be executed after the picture is taken
Camera.PictureCallback mCall = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//decode the data obtained by the camera into a Bitmap
capturedImage = BitmapFactory.decodeByteArray(data, 0, data.length);
String filename= Environment.getExternalStorageDirectory()
+ File.separator + "testimage.jpg";
FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
capturedImage.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//set the iv_image
imageView.setImageBitmap(capturedImage);
}
};

mCamera.takePicture(null, null, mCall);

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<SurfaceView
android:id="@+id/surfaceView"
android:layout_height="0dip"
android:layout_width="0dip">

</SurfaceView>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView">

</ImageView>

</LinearLayout>

更新1:

这是我的 list 文件,我忘记包含此文件:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

我还在应用程序的棉花糖设置中启用了权限,但仍然没有显示任何内容

更新2:刚刚在 API 17 设备上尝试了一下,仍然没有预览

最佳答案

假设您的所有代码实际上都按您的预期运行,一种可能性是:setPreviewSize(352, 288) - 该大小可能不受支持。

您需要检查支持的预览尺寸列表并选择一个,或者使用基本上始终可用的 320,240 或 640,480。

关于java - Android 相机预览未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39549001/

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