I have seen several other questions on this subject but none seem to solve my problem. I have a custom camera app that is working fine, everything but the zoom buttons. this is my code using SDK min 8 target 14:
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (isPreviewing){
camera.stopPreview();
}
Camera.Parameters p = camera.getParameters();
p.setPreviewSize(sizes.get(0).width, sizes.get(0).height);
p.setColorEffect(effect);
zoomControls = (ZoomControls) findViewById(R.id.zoomControls);
if (p.isZoomSupported()) {
maxZoomLevel = p.getMaxZoom();
Toast.makeText(PictureTaker.this, String.valueOf(maxZoomLevel),Toast.LENGTH_LONG).show();
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel < maxZoomLevel) {
currentZoomLevel++;
camera.startSmoothZoom(currentZoomLevel);
//Toast.makeText(PictureTaker.this, String.valueOf(currentZoomLevel),Toast.LENGTH_LONG).show();
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel > 0) {
currentZoomLevel--;
camera.startSmoothZoom(currentZoomLevel);
}
}
});
} else {
zoomControls.setVisibility(View.GONE);
}
camera.setParameters(p);
try {
camera.setPreviewDisplay(holder);
} // end try
catch (IOException e) {
Log.v(TAG, e.toString());
} // end catch
camera.startPreview(); // begin the preview
isPreviewing = true;
}
The setColorEffect is coming from the options menu and works perfectly. I know isZoomSupported and getMaxZoom are working because the Toast displays a "59" when the code runs, but the zoom buttons do nothing. This is the zoomControl from the XML
<ZoomControls
android:id="#+id/zoomControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="17dp"
android:baselineAligned="false"
android:gravity="center_horizontal"
android:orientation="horizontal" />
I have all the necessary permissions in the Manifest and no errors are showing in LogCat. Not sure what I'm doing wrong. I added a second Toast to report if the currentZoomLevel is being changed when the button is pressed and it shows the value getting incremented by one each time. I also tried not using startSmoothZoom and just setting the zoom with
p.setZoom(currentZoomLevel); or p.setZoomLevel(15);
and neither one works either. My phone, HTC Incredible does have a perfectly working zoom on its native camera app. If I comment out the zoomControl parts of the code, everything works fine and all other features of the custom camera work fine even with the zoomControl code in there, it just doesn't zoom.
Figured it out and maybe this can help some of the other people who have been having similar problems. It was the smoothzoom. Guess my HTC doesn't support this.
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (isPreviewing){
camera.stopPreview();
}
p = camera.getParameters();
p.setPreviewSize(sizes.get(0).width, sizes.get(0).height);
p.setColorEffect(effect);
if (p.isZoomSupported() && p.isSmoothZoomSupported()) {
//most phones
maxZoomLevel = p.getMaxZoom();
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel < maxZoomLevel) {
currentZoomLevel++;
camera.startSmoothZoom(currentZoomLevel);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel > 0) {
currentZoomLevel--;
camera.startSmoothZoom(currentZoomLevel);
}
}
});
} else if (p.isZoomSupported() && !p.isSmoothZoomSupported()){
//stupid HTC phones
maxZoomLevel = p.getMaxZoom();
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel < maxZoomLevel) {
currentZoomLevel++;
p.setZoom(currentZoomLevel);
camera.setParameters(p);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel > 0) {
currentZoomLevel--;
p.setZoom(currentZoomLevel);
camera.setParameters(p);
}
}
});
}else{
//no zoom on phone
zoomControls.setVisibility(View.GONE);
}
camera.setParameters(p);
try {
camera.setPreviewDisplay(holder);
} // end try
catch (IOException e) {
Log.v(TAG, e.toString());
} // end catch
camera.startPreview(); // begin the preview
isPreviewing = true;
} // end method surfaceChanged
This lets my HTC zoom in steps. The key was setting the camera's parameters after each button click. You could probably set the currentZoomLevel to any number you want, based on the maxZoomLevel, (my HTC is 59 but my Droid 4 is only 15), during the clicks of the ZoomControls to make the device zoom in and out faster. Might be a tidier way to code this, should probably put some checks in to make sure maxZoomSize doesn't return a NULL or something, but it's working on multiple devices.
Its simple forget about startSmoothZoom() method
follow this:
Camera.Parameters params = camera.getParameters();
params.setZoom(zoom_value);
camera.setParameters(params);
params.setZoom(currentZoomLevel); camera.setParameters(params); code also work at my Samsung G3 phone.
Related
I am writing an Android Camera App and trying to implement Flash functionality, I am using SurfaceView, whenever i do tap on flash button toggle works but flash not coming... see my below code i have used to Turn ON/OFF Flashlights.
Complete code:--
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
preview=(SurfaceView)findViewById(R.id.surface);
previewHolder=preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
previewHolder.setFixedSize(getWindow().getWindowManager()
.getDefaultDisplay().getWidth(), getWindow().getWindowManager()
.getDefaultDisplay().getHeight());
btnFlash = (ImageButton) findViewById(R.id.btnFlash);
/*
* First check if device is supporting flashlight or not
*/
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
AlertDialog alert = new AlertDialog.Builder(CameraLauncherActivity.this)
.create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
}
});
alert.show();
return;
}
// displaying button image
toggleButtonImage();
btnFlash.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
}
/*
* Turning On flash
*/
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(params);
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
}
/*
* Turning Off flash
*/
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
isFlashOn = false;
// changing button/switch image
toggleButtonImage();
}
}
/*
* Toggle switch button images
* changing image states to on / off
* */
private void toggleButtonImage(){
if(isFlashOn){
btnFlash.setImageResource(R.drawable.green_button);
}else{
btnFlash.setImageResource(R.drawable.red_button);
}
}
#Override
public void onResume() {
super.onResume();
camera=Camera.open();
}
#Override
public void onPause() {
super.onPause();
if (inPreview) {
camera.stopPreview(); }
camera.release();
camera=null;
inPreview=false;
}
private Camera.Size getBestPreviewSize(int width, int height,Camera.Parameters parameters){
Camera.Size result=null;
for (Camera.Size size : parameters.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);
}
SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback(){
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(previewHolder);
} catch (Throwable t) {
Log.e("PreviewDemo-surfaceCallback",
"Exception in setPreviewDisplay()", t);
Toast.makeText(CameraLauncherActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void surfaceChanged(SurfaceHolder holder,int format, int width,int height) {
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
Camera.Size size=getBestPreviewSize(width, height,
params);
if (size!=null) {
params.setPreviewSize(size.width, size.height);
camera.setParameters(params);
camera.startPreview();
inPreview=true;
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomControls);
if (params.isZoomSupported()) {
final int maxZoomLevel = params.getMaxZoom();
Log.i("max ZOOM ", "is " + maxZoomLevel);
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener(){
public void onClick(View v){
if(currentZoomLevel < maxZoomLevel){
currentZoomLevel++;
//mCamera.startSmoothZoom(currentZoomLevel);
params.setZoom(currentZoomLevel);
camera.setParameters(params);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener(){
public void onClick(View v){
if(currentZoomLevel > 0){
currentZoomLevel--;
params.setZoom(currentZoomLevel);
camera.setParameters(params);
}
}
});
}
else
zoomControls.setVisibility(View.GONE);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
};
}
A few things I can think of:
Remember to add the features and requirements on the manifest for a camera app:
<!-- Camera Requirement -->
<uses-feature android:name="android.hardware.camera" />
<!-- Camera Permissions -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- Features -->
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:name="android.hardware.camera.autofocus" />
If you want to use it as flashlight you can do what #yoah said:
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH)
If not, to turn flash on which will come out when you take the picture, you use:
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON)
I wrote a custom camera app recently, but haven't uploaded it to Github yet, it has all the basic functions working.
Try to use
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH)
Setting FLASH_MODE_ON means that flash will show when taking a picture
I have written code to work with Custom Camera and for this I am using SurfaceView, now i want to know what if i need to implement Zoom IN and OUT functionality, I never worked on this kind of app.
My code, which i used to create Custom Camera, see below:
MainActivity.java:-
public class MainActivity extends Activity {
private SurfaceView preview=null;
private SurfaceHolder previewHolder=null;
public Camera camera ;
private boolean inPreview=false;
ImageButton btnCapture;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preview=(SurfaceView)findViewById(R.id.surface);
previewHolder=preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
previewHolder.setFixedSize(getWindow().getWindowManager()
.getDefaultDisplay().getWidth(), getWindow().getWindowManager()
.getDefaultDisplay().getHeight());
btnCapture = (ImageButton) findViewById(R.id.btnCapture);
btnCapture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Capture Image(s)", Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onResume() {
super.onResume();
camera=Camera.open();
}
#Override
public void onPause() {
super.onPause();
if (inPreview) {
camera.stopPreview(); }
camera.release();
camera=null;
inPreview=false;
}
private Camera.Size getBestPreviewSize(int width, int height,Camera.Parameters parameters){
Camera.Size result=null;
for (Camera.Size size : parameters.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);
}
SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback(){
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(previewHolder);
} catch (Throwable t) {
Log.e("PreviewDemo-surfaceCallback",
"Exception in setPreviewDisplay()", t);
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void surfaceChanged(SurfaceHolder holder,int format, int width,int height) {
Camera.Parameters parameters=camera.getParameters();
Camera.Size size=getBestPreviewSize(width, height,
parameters);
if (size!=null) {
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
camera.startPreview();
inPreview=true;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
};
}
activity_main.xml:-
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splash_background" >
<android.view.SurfaceView
android:id="#+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ImageButton
android:id="#+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/capture"
android:contentDescription="#string/app_name" />
</RelativeLayout>
I found a way by using several links, it works for me, and finally i have added code into my surfaceChanged(...), my complete surfaceChanged(..) code looks like, below:
declare variable of int datatype named currentZoomLevel and assign 0 as initial value,
int currentZoomLevel = 0;
Code:-
public void surfaceChanged(SurfaceHolder holder,int format, int width,int height) {
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
Camera.Size size=getBestPreviewSize(width, height,
params);
if (size!=null) {
params.setPreviewSize(size.width, size.height);
camera.setParameters(params);
camera.startPreview();
inPreview=true;
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomControls);
if (params.isZoomSupported()) {
final int maxZoomLevel = params.getMaxZoom();
Log.i("max ZOOM ", "is " + maxZoomLevel);
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener(){
public void onClick(View v){
if(currentZoomLevel < maxZoomLevel){
currentZoomLevel++;
//mCamera.startSmoothZoom(currentZoomLevel);
params.setZoom(currentZoomLevel);
camera.setParameters(params);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener(){
public void onClick(View v){
if(currentZoomLevel > 0){
currentZoomLevel--;
params.setZoom(currentZoomLevel);
camera.setParameters(params);
}
}
});
}
else
zoomControls.setVisibility(View.GONE);
}
}
Xml:-
<ZoomControls
android:id="#+id/zoomControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
I am placing my answer, just for more clearity and for viewers future use..
Below is the sample code for zoom functionality .Please try if it can help you:
public class DemoActivity extends Activity {
private FrameLayout pCameraLayout = null; // this layout contains surfaceview
private ZoomControls zoomControls ;
Camera mCamera=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
enableZoom();
}
private void enableZoom() {
zoomControls = new ZoomControls(this);
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
zoomCamera(false);
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
zoomCamera(true);
}
});
pCameraLayout.addView(zoomControls);
}
/**
* Enables zoom feature in native camera . Called from listener of the view
* used for zoom in and zoom out.
*
*
* #param zoomInOrOut "false" for zoom in and "true" for zoom out
*/
public void zoomCamera(boolean zoomInOrOut) {
if(mCamera!=null) {
Parameters parameter = mCamera.getParameters();
if(parameter.isZoomSupported()) {
int MAX_ZOOM = parameter.getMaxZoom();
int currnetZoom = parameter.getZoom();
if(zoomInOrOut && (currnetZoom <MAX_ZOOM && currnetZoom >=0)) {
parameter.setZoom(++currnetZoom);
}
else if(!zoomInOrOut && (currnetZoom <=MAX_ZOOM && currnetZoom >0)) {
parameter.setZoom(--currnetZoom);
}
}
else
Toast.makeText(context, "Zoom Not Avaliable", Toast.LENGTH_LONG).show();
mCamera.setParameters(parameter);
}
}
I'm working on an app which has a custom camera screen, for which I'm supposed to implement tap to focus, like in the Android(more specifically, the Galaxy S4) camera app.
I've tried using the steps outlined here, but it doesn't seem to cause any noticeable focusing. The Focus Mode is set to Continuous Picture(we are supporting only a specific device).
When the user taps on the camera preview, I need to be focusing on the top half of the image. For this, I use the code snippet
Parameters parameters = mCamera.getParameters();
if (parameters.getMaxNumFocusAreas() > 0) {
ArrayList<Area> focusAreas = new ArrayList<Camera.Area>(1);
focusAreas.add(new Area(new Rect(-1000, -1000, 1000, 0), 750));
parameters.setFocusAreas(focusAreas);
mCamera.setParameters(parameters);
}
I do NOT want AutoFocus as it takes too long to focus on the image. I am interested only in the top half of the image. Has anybody successfully implemented Tap to Focus along with Continuous Picture mode?
Bumped into this issue recently. As MatheusJardimB said, this question helps a lot.
However, in my case, I wanted to start in the ContinuousPicture mode then be able to tap to focus and then continue with the ContinuousPicture mode.
I managed to get it to work by using the onAutoFocus method of the Camera.AutoFocusCallback(). I'm not sure if it's the best or the prettiest way of doing it, but it seems to work.
Here's the code:
setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (mCamera != null) {
Camera camera = mCamera.getCamera();
camera.cancelAutoFocus();
Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f);
Parameters parameters = camera.getParameters();
parameters.setFocusMode(Parameters.FOCUS_MODE_MACRO);
if (parameters.getMaxNumFocusAreas() > 0) {
List<Area> mylist = new ArrayList<Area>();
mylist.add(new Camera.Area(focusRect, 1000));
parameters.setFocusAreas(mylist);
}
camera.setParameters(parameters);
camera.autoFocus(new Camera.AutoFocusCallback() {
#Override
public void onAutoFocus(boolean success, Camera camera) {
camera.cancelAutoFocus();
Parameters params = camera.getParameters();
if (!params.getFocusMode().equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(params);
}
}
});
}
return true;
}
return false;
});
You could just change the focus area to
ArrayList<Area> focusAreas = new ArrayList<Camera.Area>(1);
focusAreas.add(new Area(new Rect(-1000, -1000, 1000, 0), 750));
and it should work.
UPDATE
I recently acquired a Samsung S5 and tested this out on it. It didn't work that well, so I added a few modifications and it's working now. This was also successfully tested on the Galaxy S6 and Galaxy Note4.
Here's the modified code:
setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (mCamera != null) {
Camera camera = mCamera.getCamera();
camera.cancelAutoFocus();
Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f);
Parameters parameters = camera.getParameters();
if (parameters.getFocusMode().equals(
Camera.Parameters.FOCUS_MODE_AUTO) {
parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
}
if (parameters.getMaxNumFocusAreas() > 0) {
List<Area> mylist = new ArrayList<Area>();
mylist.add(new Camera.Area(focusRect, 1000));
parameters.setFocusAreas(mylist);
}
try {
camera.cancelAutoFocus();
camera.setParameters(parameters);
camera.startPreview();
camera.autoFocus(new Camera.AutoFocusCallback() {
#Override
public void onAutoFocus(boolean success, Camera camera) {
if (!camera.getParameters().getFocusMode().equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
Parameters parameters = camera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
if (parameters.getMaxNumFocusAreas() > 0) {
parameters.setFocusAreas(null);
}
camera.setParameters(parameters);
camera.startPreview();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
});
this has the solution. I just added the implementation of some missing methods in his code.
private static final int FOCUS_AREA_SIZE= 300;
//
mCameraPreview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
focusOnTouch(event);
}
return true;
}
});
//
private void focusOnTouch(MotionEvent event) {
if (mCamera != null ) {
Camera.Parameters parameters = mCamera.getParameters();
if (parameters.getMaxNumMeteringAreas() > 0){
Log.i(TAG,"fancy !");
Rect rect = calculateFocusArea(event.getX(), event.getY());
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>();
meteringAreas.add(new Camera.Area(rect, 800));
parameters.setFocusAreas(meteringAreas);
mCamera.setParameters(parameters);
mCamera.autoFocus(mAutoFocusTakePictureCallback);
}else {
mCamera.autoFocus(mAutoFocusTakePictureCallback);
}
}
}
private Rect calculateFocusArea(float x, float y) {
int left = clamp(Float.valueOf((x / mCameraPreview.getWidth()) * 2000 - 1000).intValue(), FOCUS_AREA_SIZE);
int top = clamp(Float.valueOf((y / mCameraPreview.getHeight()) * 2000 - 1000).intValue(), FOCUS_AREA_SIZE);
return new Rect(left, top, left + FOCUS_AREA_SIZE, top + FOCUS_AREA_SIZE);
}
private int clamp(int touchCoordinateInCameraReper, int focusAreaSize) {
int result;
if (Math.abs(touchCoordinateInCameraReper)+focusAreaSize/2>1000){
if (touchCoordinateInCameraReper>0){
result = 1000 - focusAreaSize/2;
} else {
result = -1000 + focusAreaSize/2;
}
} else{
result = touchCoordinateInCameraReper - focusAreaSize/2;
}
return result;
}
// implement this callback to trigger the focus.
private Camera.AutoFocusCallback mAutoFocusTakePictureCallback = new Camera.AutoFocusCallback() {
#Override
public void onAutoFocus(boolean success, Camera camera) {
if (success) {
// do something...
Log.i("tap_to_focus","success!");
} else {
// do something...
Log.i("tap_to_focus","fail!");
}
}
};
One of the other answers causes your camera to discard the focus points previously given and return to continuous focus which I don't think makes sense.
Also if you take a look at the link in the post the original answer uses parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
I've implemented the above with this line of code instead of Continuous focus and it seems to work much better.
binding.cPreview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCamera.autoFocus(myAutoFocusCallback);
}
});
Camera.AutoFocusCallback myAutoFocusCallback = new Camera.AutoFocusCallback(){
#Override
public void onAutoFocus(boolean arg0, Camera arg1) {
// TODO Auto-generated method stub
}};
I have the same issue.
I've checked this on many devices, and many Android versions.
It appears that focus-area is not working in CONTINUOUS focus-mode.
My workaround is to set focus-mode to AUTO or MACRO along with setting focus-area:
params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
params.setFocusAreas(focusAreas);
mCamera.setParameters(params);
Please note that the stock Camera app on Galaxy S3 & S4 works the same way: it's permanently in continuous mode. When you touch the screen it switches to auto & sets focus-area. But after a while it comes back to continuous mode, and focus-area goes back to the center of the screen.
I hope this helps you somehow.
I am working on Custom Camera using Surface view in my application, also recording Video with screen Capture.
I am looking to implement Zoom Control in my Camera Code but not getting success.
Getting this log while Zooming:
E/Adreno200-ES20(12756): <qgl2DrvAPI_glBufferSubData:709>: GL_INVALID_OPERATION
I am using below code:
/**
* setZoomControl
* #param params
*/
public void setZoomControl(Camera.Parameters params) {
paramtrs = params;
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
if (params.isZoomSupported()) {
final int maxZoomLevel = paramtrs.getMaxZoom();
Log.i("max ZOOM ", "is " + maxZoomLevel);
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
int currentZoomLevel = 0;
if (currentZoomLevel < maxZoomLevel) {
currentZoomLevel++;
camera.startSmoothZoom(currentZoomLevel);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
int currentZoomLevel = 0;
if (currentZoomLevel > 0) {
currentZoomLevel--;
camera.startSmoothZoom(currentZoomLevel);
}
}
});
} else
zoomControls.setVisibility(View.GONE);
}
Please suggest me whats happening in my code.
You can use this code
if (params.isZoomSupported()) {
final int maxZoomLevel = params.getMaxZoom();
Log.i("max ZOOM ", "is " + maxZoomLevel);
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener(){
public void onClick(View v){
if(currentZoomLevel < maxZoomLevel){
currentZoomLevel++;
//mCamera.startSmoothZoom(currentZoomLevel);
params.setZoom(currentZoomLevel);
mCamera.setParameters(params);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener(){
public void onClick(View v){
if(currentZoomLevel > 0){
currentZoomLevel--;
params.setZoom(currentZoomLevel);
mCamera.setParameters(params);
}
}
});
}
else
zoomControls.setVisibility(View.GONE);
I have this function to add zoom control to my camera app. This function is called from
surfaceChanged()
But on many of the devices pressing + / - for zoom out / in shows no effect. These devices support Zoom . Also the code is working on sony xperia , so the code must be correct.
Please help me to fix it. Is there anything more i need to do .
public void setZoomControl(Camera.Parameters params) {
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.CAMERA_ZOOM_CONTROLS);
if (params.isZoomSupported()) {
maxZoomLevel = params.getMaxZoom();
Log.i("max ZOOM ", "is " + maxZoomLevel);
;
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel < maxZoomLevel) {
currentZoomLevel++;
camera.startSmoothZoom(currentZoomLevel);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel > 0) {
currentZoomLevel--;
camera.startSmoothZoom(currentZoomLevel);
}
}
});
} else
zoomControls.setVisibility(View.GONE);
}
I solved this problem.
Don't used startSmoothZoom() method
Camera.Parameters params = camera.getParameters();
params.setZoom(zoom_value);
camera.setParameters(params);