I found a tutorial online here both for capturing images, they are very similar and I used one another to figure out why my Camera code isn't working.
I do not get any syntax errors in Android but when I go on the desired fragment it is just a white screen, there is no camera display and I have no idea why I have looked at both code examples in depth and googled my problem but cant find anything. The only difference with my code is that its a fragment instead of an activity. Can someone please help me?
Here is my code:
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;
}
}
Here is my XML file:
<?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>
Update 1:
Here is my manifest file, I forgot to include this:
<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" />
I have also enabled the permissions in marshmallow settings for the application but still doesn't show anything
Update 2: Just tried it with a API 17 device and there is still no preview
请确保已将必需的相机权限添加到AndroidManifest.xml文件中;如果您使用的是蛋白软糖,请再检查一步,从设置=>应用程序=>应用程序管理器=>您的应用程序=>权限中启用权限
Assuming all your code is actually running as you expect, one possibility: setPreviewSize(352, 288)
- that size may not be supported.
You'll need to check the list of supported preview sizes and pick one, or use 320,240 or 640,480 which are basically always available.
在XML中,android_height和android_width为0,将其更改!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.