Surfaceview - > Surface Had No Valid Native Window | Android Camera Api | Hardware.camera2
Solution 1:
After many days and sleepless nights of battling with the camera2 api, I came to the conclusion that the documentation and even the samples are not crystal clear, or, I am just slow on the uptake. Consolation for me came when I saw lots of people are battling with the api and replies to issues raised does not necessary provide solutions and simple answers.
My attempt was to take two pictures - one after the other. Even closing the Intent with finish() and reopening it, did not provide the solution - I simply could not take a second picture without relaunching the entire application. On the second picture, the API kept on failing with the following issue:-
FatalException: java.lang.IllegalArgumentExceptionSurfacehadnovalidnativeSurface.
android.hardware.camera2.legacy.LegacyCameraDevice.nativeGetSurfaceId (LegacyCameraDevice.java)
android.hardware.camera2.legacy.LegacyCameraDevice.getSurfaceId (LegacyCameraDevice.java:658)
android.hardware.camera2.legacy.LegacyCameraDevice.containsSurfaceId (LegacyCameraDevice.java:678)
These lines of code provided the clue and my breakthrough:-
ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
outputSurfaces.add(reader.getSurface());
outputSurfaces.add(new Surface(textureView.getSurfaceTexture()));
We retrieve the bitmap and save it to a file more or less as follows:-
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = newbyte[buffer.capacity()];
buffer.get(bytes);
Then finally:-
if (image != null) {
image.close();
}
Overlooked by many:-
ImageReaderreader= ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
For some reason, this remains running as an instance and will continue to do so until it is closed. It does not recycle with the GC and it does not get destroyed when the class is closed. You should manually close the reader as well:-
if (image != null) {
image.close();
}
if(reader != null) {
reader.close();
}
It solved all my camera2 issues I am sure only until next time. Hope this helped somebody else as well!
Solution 2:
After lots of hours I realised I just needed to reach SurfaceCreated() and I didn't reach it, solution was to not openCamera in onCreate().
Solution 3:
i found solution use this method to choose your size
privatestatic Size chooseOptimalSize(Size[] choices, int width, int height) {
SizebigEnough=null;
intminAreaDiff= Integer.MAX_VALUE;
for (Size option : choices) {
intdiff= (width*height)-(option.getWidth()*option.getHeight()) ;
if (diff >=0 && diff < minAreaDiff &&
option.getWidth() <= width &&
option.getHeight() <= height) {
minAreaDiff = diff;
bigEnough = option;
}
}
if (bigEnough != null) {
return bigEnough;
} else {
Arrays.sort(choices,newCompareSizeByArea());
return choices[0];
}
}
Solution 4:
you just need to call "openCamera" function after your surfaceView Created
put this code to onCreate ( this code is for kotlin )
mySurfaceView!!.holder!!.addCallback(object: SurfaceHolder.Callback {
overridefunsurfaceChanged(holder: SurfaceHolder, format: Int,
width: Int, height: Int) {
}
overridefunsurfaceCreated(holder: SurfaceHolder) {
openCamera()
}
overridefunsurfaceDestroyed(holder: SurfaceHolder) {
}
})
Post a Comment for "Surfaceview - > Surface Had No Valid Native Window | Android Camera Api | Hardware.camera2"