To reduce the capacity of the picture, I used setPictureSize as shown below to size the picture, but after doing camera.takePicture() there was nothing else.
What can I do to reduce the size of the pictures taken from the camera?
Camera camera = getCameraInstance();
final Camera.Parameters parameters = camera.getParameters();
parameters.setPictureSize(640,320);
You also have set the parameters after you have set the picture size.
And you can also reduce jpeg quality if you want to reduce the image size even more.
this is how I did it
mCamera = getCameraInstance();
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();
Camera.Size size = sizes.get(0);
for(int i=0;i<sizes.size();i++)
{
if(sizes.get(i).width >= 800 && sizes.get(i).width<=1000){
size = sizes.get(i);
}
}
params.setJpegQuality(30);
params.setPictureSize(size.width,size.height);
mCamera.setParameters(params);
Related
I'm trying to use camera features as part of my application and I'm stuck on camera preview step.
I want to understand why the preview image remains dark if there are no bright light.
Here is what params I set before start previewing:
mParameters = mCamera.getParameters();
List<Camera.Size> mSupportedPreviewSizes = mParameters.getSupportedPreviewSizes();
Camera.Size optimalSize = CameraHelper.getOptimalPreviewSize(mSupportedPreviewSizes, DEFAULT_PREVIEW_WIDTH, DEFAULT_PREVIEW_HEIGHT);
// Use the same size for recording profile.
mProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mProfile.videoFrameWidth = optimalSize.width;
mProfile.videoFrameHeight = optimalSize.height;
// likewise for the camera object itself.
mParameters.setPreviewSize(mProfile.videoFrameWidth, mProfile.videoFrameHeight);
// Set correct video width and height according to screen rotation
transformMatrixHelper.setVideoDimensions(optimalSize.width, optimalSize.height);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
transformMatrixHelper.setVideoDimensions(optimalSize.height, optimalSize.width);
}
transformMatrixHelper.clearInitTextureDimension();
mParameters.setPreviewFpsRange(MAX_FPS, MAX_FPS);
mParameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
// Auto-focus
if (mParameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
mParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
// Auto-exposure
if (mParameters.isAutoExposureLockSupported()) {
mParameters.setAutoExposureLock(false);
}
mCamera.setParameters(mParameters);
I'm not calling any camera.autoFocus(callback) method after preview stared.
I will be very grateful if someone help me, thanks.
I have the following code in my app:
camera = Camera.open(CameraInfo.CAMERA_FACING_FRONT);
Camera.Parameters setting_param = camera.getParameters();
setting_param.setPreviewSize(128,128);
camera.setParameters(setting_param);
Camera.Parameters read_param = camera.getParameters();
Camera.Size sz = read_param.getPreviewSize();
The code works exactly as expected on my galaxy tab 10.1 running Android 3.1. but on My Galaxy S II phone running 4.1.2, the width and height of the preview, i.e. sz.width and sz.height are 640 and 480! Is this a bug in 4.1.2, or a consequence of an error of mine I am getting away with in 3.1 and not getting away with in 4.1.2?
Before setting a preview size you should check if that preview size is supported.
You have a method on Camera called getSupportedPreviewSizes which will return you a list of supported preview sizes.
Mick, you should check if a preview size is supported by camera before setting it. Change your onSurfaceChanged method a bit like following.
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters mParameters = mCamera.getParameters();
Camera.Size bestSize = null;
List<Camera.Size> sizeList = mCamera.getParameters().getSupportedPreviewSizes();
bestSize = sizeList.get(0);
for(int i = 1; i < sizeList.size(); i++){
if((sizeList.get(i).width * sizeList.get(i).height) >
(bestSize.width * bestSize.height)){
bestSize = sizeList.get(i);
}
}
mParameters.setPreviewSize(bestSize.width, bestSize.height);
mCamera.setParameters(mParameters);
mCamera.startPreview();
//any other code ....
}
I'm using camera on my app. Camera is working perfect on all devices upto Samsung S3 even. Image is correct from all other devices.
While taking image from S4 , image gets corrupted and image gets saved with some lines in horizontal.
I tried changing resolution and everything but still issue is there .
Any help
I've been pulling my hair out over this and I think I found the issue, at least with regards to my app - it's got something to do with the aspect ratio of the preview image versus the captured image.
In my case, my code was sniffing out the ideal preview size based on the aspect ratio of the screen. The S4 is a 1080p phone, so the preview image was 1920x1080, which is a 16:9 aspect ratio. But my code was hardcoded to capturing a 1600x1200 image, which is 4:3, because that's all I needed. But 1600x1200 is not one of the valid sizes the S4 supports.
Without setting the size, the S4 captured 4128x3096, which is the maximum size, and is 4:3, but the lines still appeared. Once I told the camera to capture a 16:9 photo, the lines went away. In your case, you might want to adjust the preview's aspect ratio.
Here's some code which can tell you the available sizes.
List<Camera.Size> previewSizes = p.getSupportedPreviewSizes();
int i = 1;
for (Size previewSize : previewSizes) {
Log.v("DebugCamera", "previewSize " + i++ + " width: " + previewSize.width + " height: " + previewSize.height);
}
Just tried this code on S4 and it works. Try it:
private Camera.Size getBestPreviewSize(int width, int height)
{
Camera.Size result=null;
Camera.Parameters p = camera.getParameters();
for (Camera.Size size : p.getSupportedPreviewSizes()) {
if (size.width<=width && size.height<=height) {
if (result==null) {
result=size;
} else {
int resultArea=result.width*result.height;
int newArea=size.width*size.height;
if (newArea>resultArea) {
result=size;
}
}
}
}
return result;
}
public void surfaceCreated(SurfaceHolder holder) {
if(myCamera == null){
myCamera = getCameraInstance();
try {
myCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
// Surface will be destroyed when we return, so stop the preview.
if (myCamera != null)
{
myCamera.stopPreview();
myCamera.setPreviewCallback(null);
myCamera.release();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//This line helped me set the preview Display Orientation to Portrait
//Only works API Level 8 and higher unfortunately.
camera.setDisplayOrientation(90);
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = getBestPreviewSize(width, height);
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
camera.startPreview();
}
I have built a custom Camera App and I am trying to change the resoloution of the image that is took. I have read around that this could depend on the phone or version of Android?
I know they are set using the setParameters but just dont know how to set the actuall resoloution to work on all phones. I am wanting it to be kind of small as my app force closes otherwise. When I use a test picture at 640x348 this works so around that size/resoloution would be perfect.
It may be easier to use setPictureSize?
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
try {
Camera.Parameters parameters = camera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "portrait");
camera.setDisplayOrientation(90);
// Uncomment for Android 2.0 and above
parameters.setRotation(90);
} else {
parameters.set("orientation", "landscape");
camera.setDisplayOrientation(0);
// Uncomment for Android 2.0 and above
parameters.setRotation(0);
}
camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
} catch (IOException exception) {
camera.release();
}
camera.startPreview();
}
There is no setResolution(), only setPictureSize(). Use getSupportedPictureSizes() on Camera.Parameters to find the size you want, or use that information to populate a ListView or Spinner or something for the user to choose the desired size. Here is a sample project recently updated to use getSupportedPictureSizes() to find the smallest supported resolution and use that.
It's too easy to capture image with high quality, here you can set your own resolution:
mCamera = Camera.open();
Camera.Parameters params = mCamera.getParameters();
// Check what resolutions are supported by your camera
List<Size> sizes = params.getSupportedPictureSizes();
// Iterate through all available resolutions and choose one.
// The chosen resolution will be stored in mSize.
Size mSize;
for (Size size : sizes) {
Log.i(TAG, "Available resolution: "+size.width+" "+size.height);
mSize = size;
}
}
Log.i(TAG, "Chosen resolution: "+mSize.width+" "+mSize.height);
params.setPictureSize(mSize.width, mSize.height);
mCamera.setParameters(params);
Hope this will help you all.
I have the following problem:I have an app where I'm using the camera of the android device.
I have built my own camera.The BIG problem is that when the preview starts all the image looks resized.Every object looks longer, larger...
in surfaceChanged() method I've done this:
List<Size> previews = p.getSupportedPreviewSizes();
I looped this list previews but I haven't found a better size for my preview.
The list previews has the size equal to 7 but nne of this items make my image look better!!
Here is how my method looks like:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.e(TAG, "surfaceChanged");
if (mPreviewRunning) {
mCamera.stopPreview();
}
Camera.Parameters p = mCamera.getParameters();
List<Size> sizes = p.getSupportedPictureSizes();
List<Size> previews = p.getSupportedPreviewSizes();
Size preview = previews.get(3);
// the size of preview is 7 and looped each item....
p.setPreviewSize(preview.width,preview.height);
int f=p.getJpegQuality();
Size size = sizes.get(3);
p.setJpegQuality(f);
p.setPictureSize(size.width,size.height);
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
Anyone any idea?
Take a look at the PreviewFrameLayout class from the default Camera app. It's used to display SurfaceView using the aspect ratio it must have.
You need to maintain your aspect ratio.
Try setting your preview size to 640x480 if your device supports it. This will maintain an aspect ratio of 4:3.
If your dev doesn't support 640x480 try setting it to a different resolution which maintains an aspect ratio of 4:3.
You can set the preview size on the camera like this -
mCameraDevPara.setPreviewSize(PREVIEW_WIDTH, PREVIEW_HEIGHT);
mCameraDev.setParameters(mCameraDevPara);
Only after you set the preview size you can call the API mCameraDev.setPreviewDisplay(mSurfaceHolder); and mCameraDev.startPreview();