How to get supported video camera resolutions in android? - java

I am writing an app where I am allowing the user to capture video using the phones camera. I am using my own code to record the video as opposed to Androids built in camera app.
Everything is working OK except I need to be able to access the list of supported camera resolutions so I can choose at runtime which one to use. I am looking for something like getSupportedPictureSizes() but for video. Android 3.0 has this functionality but I am looking for something for 2.2.
As of right now I am using CamcorderProfile.QUALITY_HIGH / QUALITY_LOW, but this only gives me two options and on the phones I have been testing on, the file sizes are at each extreme.(QUALITY_LOW is 216 kb/s and QUALITY_HIGH is > 3 MB/s)
Any help would be greatly appreciated,
Thank You!

Did you try to use the getSupportedVideoSizes() method from Camera.Parameters class?
public List<Camera.Size> getSupportedVideoSizes()
This method returns a list of Size objects. It will return null if the camera does not have separate preview and video output. The answer here indicates that when this returns null you may use the getSupportedPreviewSizes() list.

Ok I think I figured it out. It seems to work correctly on the phones I have been testing on.
List<Size> tmpList = camera.getParameters().getSupportedPreviewSizes();
final List<Size> sizeList = new Vector<Size>();
//compair the apsect ratio of the candidate sizes against the real ratio
Double aspectRatio = (Double.valueOf(getWindowManager().getDefaultDisplay().getHeight()) / getWindowManager().getDefaultDisplay().getWidth());
for(int i=0; i<tmpList.size(); i++){
Double tmpRatio = Double.valueOf(tmpList.get(i).height) / tmpList.get(i).width;
if(Math.abs(aspectRatio - tmpRatio) < .15){
sizeList.add(tmpList.get(i));
}
}

Related

Android Camera2 Preview Image Decoding fails on car head unit

I am pretty new to Camera2 API and currenty trying to implement a camera preview into my own car application on an Android head unit (Model YT9213AJ). The preview should show the image of the reverse camera.
I've tested the following code on a Samsung Tablet (SM-P610) and it shows the camera preview images as expected, from both, rear and front camera.
private void bindPreview(#NonNull ProcessCameraProvider cameraProvider) {
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_FRONT)
.build();
CameraManager manager = (CameraManager) context.getSystemService(CAMERA_SERVICE);
Size previewSize = getPreviewSize(manager.getCameraCharacteristics("1"));
Preview preview = new Preview.Builder()
.setTargetResolution(previewSize)
.setDefaultResolution(previewSize)
.setMaxResolution(previewSize)
.setTargetRotation(Surface.ROTATION_270)
.build();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, preview);
}
With this function to get the preview size:
Size getPreviewSize(CameraCharacteristics characteristics) {
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size[] previewSizes = map.getOutputSizes(SurfaceTexture.class);
return previewSizes[0]; // for the camera just one resolution is given, so this should be sufficient
}
This is what the image looks like, if I run it on the car head unit:
Example image preview from car head unit
(Sorry, can't embed images into post yet)
I've also run the App "Camera2 API probe", please find the results here on AirBeat. The camera with ID 5 seems to be an placeholder, I assume that the other two cameras (ID 0 and ID 1) are the representations of the two hardware inputs into the head unit.
Do you have any clue how I can correctly decode the image for this camera model? Thanks for your time.
You could try seeing if not rotating the output helps - the size listed by the camera is already portrait aspect ratio.
In general, though, it looks like whoever made this head unit didn't take a lot of care in making sure the camera output works with the display side, I'm guessing. So it may not be possible to get it to work with the CameraX PreviewView as-is. The PreviewView tries to select the best kind of output View for the API level that the device is, but it may be picking a path the manufacturer didn't actually test with. There's no built-in way to have it select the other option (SurfaceView or TextureView are the choices), so you'd have to modify PreviewView code or build your own Preview surface provider.
There's also no guarantee that'll help, depending on exactly how badly this device implements things.

Exoplayer not playing large local videos correctly

I'm using Exoplayer to play local videos. Videos of small sizes <50mb are playing correctly but if I try to play a movie, it starts displaying several issues including :
Not being able to drag timebar to a specific position
Displaying the wrong total duration
Video playing slowly for a start then stopping entirely.
This is the code I'm using to initialize my player
if (currentPosition != null) {
MEDIA_ITEMS = new ArrayList<>();
for (Video video : videoList) {
MEDIA_ITEMS.add(MediaItem.fromUri(video.getPath()));
}
video = videoList.get(currentPosition);
textViewTitle.setText(video.getTitle());
simpleExoPlayer.addMediaItems(MEDIA_ITEMS);
simpleExoPlayer.seekTo(currentPosition, position.getDuration());
simpleExoPlayer.prepare();
simpleExoPlayer.play();
}
screenshot of Exoplayer showing wrong total duration (circled)
The correct duration to show is 2:12:5
How do I fix this, everything is working perfectly apart from this large video issue. Your answers are highly appreciated.
Thank you

Camera VIew OpenCV in Android Cannot FIt

I have a problem with cameraview opencv on android, on android device type samsung camera view doesn't fit there is a black cut like picture 1 while on android device type xiaomi and realme it's safe like picture 2. I took the middle resolution from supportPreviewSize and set the maxFrameSize to a ratio of 1:1, how can the camera view size be compatible with all current android devices? is this purely because my code is still wrong or is it the camera settings of the android device itself?
Picture 1 Device Samsung A51
Picture 2 Device Realme 3
setResolution() :
mCamera = android.hardware.Camera.open();Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> listSizes = params.getSupportedPreviewSizes();
List<Camera.Size> listCapture = params.getSupportedPictureSizes();
int midResolution = listSizes.size() / 2;
cameraSize = listSizes.get(midResolution);
params.setPictureSize(cameraSize.width, cameraSize.height);
params.setVideoStabilization(true);
params.setPreviewSize(cameraSize.width, cameraSize.height);
mCamera.setParameters(params);
mCamera.startPreview();
setMaxFrameSize :
jCameraView.setResolution();
Camera.Size sizeMaxFrame = jCameraView.getSizeCamera();
jCameraView.setMaxFrameSize(sizeMaxFrame.height, sizeMaxFrame.height);
I set the maxFrameSize value to the same value using the height value from the setResolution() method
Sorry if my language or question is not easy to understand
This is because of the Camera2 framework which you are using . It is really a pain to work with that frameWork and provide support to all devices. To cope up with this issue the Android Team came up with new library for the same , which is CameraX .
The documentation for the same are here :
https://developer.android.com/training/camerax
It provides rich support to most of the devices. And has lot of extra features which are known as Vendor Extensions , you will find more on it at the above link . This framework is built on top of Camera / Camera2 framework . So you should consider migrating to CameraX for better support to more devices , using the same code . Or you need to cater individual set of devices using the current framework .

How to get Java Drawables from a Plugin in Unity

I'm trying to make a launcher for cardboard apps in Unity.
My problem is that Unity doesn't provide a way to get all the VR apps installed in the phone (at least I don't know any), so I tried to make a plugin in Java to do that. Everything works fine, I can get the list of ApplicationInfo in Java and that way I can send information to Unity such as the number of these and their names.
My problem is that i can't send the image of the Icon of the apps, as Java uses a Drawable to get it from PackageManager.getApplicationIcon(), I can't access to it from Unity.
I've tried this:
private List<ApplicationInfo> cbApps; //list of ApplicationInfo already initialized
public int[] getIcon(int i) {
BitmapDrawable icon= (BitmapDrawable)getPackageManager().getApplicationIcon(cbApps.get(i));
int[] result= new int[icon.getIntrinsicWidth()*icon.getIntrinsicHeight()];
int p = 0;
for(int j=0;j<icon.getIntrinsicWidth();j++)
for(int k=0;k<icon.getIntrinsicHeight();k++){
result[p]=icon.getBitmap().getPixel(j,k);
}
return result;
}
But it only returns an array full of zeros (and sometimes a very bi positive or negative number) so it's not working as expected.
Any ideas on how to solve this?
Just for if someone comes to this queston I already found a solution and posted it in this other ask

How to set volume of a SourceDataLine in Java

I'm trying to make an mp3 player in java and I can`t figure out how to control the volume in it.
I've tried something like this:
// Adjust the volume on the output line.
if (dataLine.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
FloatControl volume = (FloatControl) dataLine.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(100.0F);
}
Everything I have written until this code worked fine but apparently the dataLine is NOT control Supported because it jumps over this IF statement.
My question is this : Do you have any idea why this is happening and how could I work this issue around so that I could control the volume of my application?
OK GUYS,
I found my mess-up. I actually forgot to call the dataLine.open(audioFormat) function which acquires the system resources.
So the code workes just fine, in case anyone has this kind of problems too
Have you tried to see what dataLine.getControls() will return ?
Obtains the set of controls associated
with this line. Some controls may only
be available when the line is open. If
there are no controls, this method
returns an array of length 0.
If you want volume wouldn't you want to test for the FloatControl.Type.VOLUME control ?
float vol=50;
final FloatControl volumeControl = (FloatControl) auline.getControl( FloatControl.Type.MASTER_GAIN );
volumeControl.setValue( 20.0f * (float) Math.log10( vol / 100.0 ) );
vol=0 means mute.

Categories

Resources