I am using following code to turn on camera flashlight
public Flashlight(SurfaceView preview, Context context){
this.preview = preview;
this.context = context;
mHolder = preview.getHolder();
mHolder.addCallback(this);
mCamera = Camera.open();
cameraOpened = true;
try {
mCamera.setPreviewDisplay(mHolder);
}catch (Exception e){
e.printStackTrace();
}
params = mCamera.getParameters();
mCamera.startPreview();
}
private void turnOnFlashlight(){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
}
private void turnOffFlashlight(){
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mHolder = holder;
try {
mCamera.setPreviewDisplay(mHolder);
}catch(Exception e){
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
mHolder = null;
}
it uses surface view and it works great on many phones however I cant get it to work on galaxy S3. Have you experienced similary problems? Any ideas how to get it working on S3?
Thanks in forward
Related
I have a simple class called CameraPreview, this is the code:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera = Camera.open();
} catch (RuntimeException e) {}
try {
SurfaceHolder someHolder = holder;
mCamera.setPreviewDisplay(someHolder);
mCamera.startPreview();
} catch (IOException e) {
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null)
return;
try {
mCamera.stopPreview();
} catch (Exception e){}
mCamera.setDisplayOrientation(90);
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){}
}
}
It's obvious that, while the application is showing the CameraPreview on screen, it will consume battery faster, my question is,what will happen to the battery if I set this view to View.GONE?
According to this SO answer setting the SurfaceView visbility to GONE will call surfaceDestroyed method.
Therefore, since you are stopping the camera, you won't consume user's battery.
I want to use camera in my app but dont want to take photo, actually I am making a app i.e transparent screen, in this I want to show transparent wallpaper i.e I have to start camera for this and i dont want to take images for this
I tried all these codes but have n't got the desirable results. Can anyone suggest what must i do?
used this permission in all cases
<uses-permission android:name="android.permission.CAMERA"/>
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
Intent intent = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
startActivity(intent);
Intent intent = new Intent(android.provider.MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivity(intent);
Update 1:
I tried this code it showing camera in not correct way, its diverting the preview to right kindly look over this updated code and tell wht amendment i can make over this
public class MainActivity extends Activity {
private Preview mPreview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Create our Preview view and set it as the content of our activity.
mPreview = new Preview(this);
setContentView(mPreview);
}catch(Exception e){
e.printStackTrace();
}
}
******************************************************************************************
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
try{
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}catch(Exception e){
e.printStackTrace();
}
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
try{
if(mCamera!=null){
mCamera.release();
mCamera=null;
}
mCamera = Camera.open();
Log.i("Camera", "Camera is opened");
mCamera.setPreviewDisplay(holder);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
try{
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}catch(Exception e){
e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
try{
Parameters parameters = mCamera.getParameters();
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = sizes.get(0);
parameters.setPreviewSize(cs.width, cs.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
}catch(Exception e){
e.printStackTrace();
}
}
}
You need to use surfaceView for this. Here is an example:
public class CameraPreview extends Activity {
private Preview mPreview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Create our Preview view and set it as the content of our activity.
mPreview = new Preview(this);
setContentView(mPreview);
}
}
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = sizes.get(0);
parameters.setPreviewSize(cs.width, cs.height);
camera.setParameters(parameters);
mCamera.startPreview();
}
}
I used this code and it worked...
<uses-permission android:name="android.permission.CAMERA"/>
public class MainActivity extends Activity {
private Preview mPreview;
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Create our Preview view and set it as the content of our activity.
mPreview = new Preview(this);
setContentView(mPreview);
}catch(Exception e){
e.printStackTrace();
}
}
}
*****************************************************************************************
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
try {
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} catch (Exception e) {
e.printStackTrace();
}
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
try {
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
mCamera = Camera.open();
Log.i("Camera", "Camera is opened");
mCamera.setPreviewDisplay(holder);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
try {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
} catch (Exception e) {
e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
try {
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("orientation", "portrait");
mCamera.setDisplayOrientation(90);
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = sizes.get(0);
parameters.setPreviewSize(cs.width, cs.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Having a little trouble getting my SurfaceView to show my camera preview. I've looked at some questions on here and Google'd some tuts but I think it may be a small error on my end that I'm just not seeing.
Code
public class RoofPitchActivity extends Activity implements SensorEventListener {
...
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roof_pitch);
initViews();
}
private void initViews() {
...
Preview preview = new Preview(this);
mCamera = Camera.open();
preview.setCamera(mCamera);
}
...
...
class Preview extends ViewGroup implements SurfaceHolder.Callback {
public Preview(Context context) {
super(context);
mSurfaceView = (SurfaceView) findViewById(R.id.preview);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
}
public void setCamera(Camera camera) {
if (mCamera == camera) {
return;
}
stopPreviewAndFreeCamera();
mCamera = camera;
if (mCamera != null) {
requestLayout();
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
}
private void stopPreviewAndFreeCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera.startPreview();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mSurfaceView.getWidth(), mSurfaceView.getHeight());
requestLayout();
mCamera.setParameters(parameters);
mCamera.startPreview();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
}
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
}
So when the activity is launched the SurfaceView is black. The camera does not appear to be rendering the preview onto the SurfaceView. I'm sure there's something small I'm missing, or maybe it's just a fundemental misunderstanding of how this works. A fresh set of eyes with a little explanation would be very much appreciated. Thanks
So the answer to my question ended up being device specific. The code was acceptable but my device I'm using to debug is a Nexus 7. This means the camera is front facing and the normal call to the camera does not work. An interesting and funny read about this can be found in this article
Camera.open();
does not work. You must use
Camera.open(0);
So my code ended up with a conditional check for the camera and now it works fine.
Like so
public void setCamera() {
stopPreviewAndFreeCamera();
PackageManager pm = getPackageManager();
boolean backCamera = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
boolean frontCamera = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
if (frontCamera) {
mCamera = Camera.open(0);
} else if (backCamera) {
mCamera = Camera.open();
}
if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mSurfaceView.getWidth(), mSurfaceView.getHeight());
requestLayout();
try {
mCamera.setParameters(parameters);
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
}
I am making an app for reading small text but i am not able to get sharp focus . i am using SCENE_MODE_BARCODE for scene mode for small text but it seems to be not working i tried autofocus also but no result either.
sorry for bad english.
my preview class code is
public class MyView extends SurfaceView implements SurfaceHolder.Callback {
Camera surfacecmr;
SurfaceHolder mholder;
#SuppressWarnings("deprecation")
public MyView(Context context, Camera cmr) {
super(context);
surfacecmr = cmr;
mholder = getHolder();
mholder.addCallback(this);
mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
// surfacecmr.setDisplayOrientation(90);
surfacecmr.setPreviewDisplay(holder);
surfacecmr.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (mholder.getSurface() == null)
return;
surfacecmr.stopPreview();
// get Camera parameters
Camera.Parameters params = surfacecmr.getParameters();
params.setSceneMode(Camera.Parameters.SCENE_MODE_BARCODE);
surfacecmr.setParameters(params);
try {
surfacecmr.setPreviewDisplay(mholder);
} catch (IOException e) {
e.printStackTrace();
}
surfacecmr.startPreview();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
surfacecmr.release();
}
}
i'm trying to create cameraPreview, and I found in the below code "Camera.open()"
this method is not accessible or not available to me, there no such method the Object Camera
can have access to.
Is there any thing i should do, or it's amistake inthe tutorial?
Java Code:
#Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
//Open the Camera in preview mode
this.camera = Camera.open();
this.camera.setPreviewDisplay(this.holder);
}
catch(IOException ioe)
{
ioe.printStackTrace(System.out);
}
}
You most likely imported the wrong camera class at the top of your source file, which is android.graphics.Camera.
You need android.hardware.Camera instead.
Here is complete Camera view class ::
class Preview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
Preview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
camera.setDisplayOrientation(90);
try {
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Log.d(TAG, "onPreviewFrame called at: "
+ System.currentTimeMillis());
Preview.this.invalidate();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
camera.release();
camera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
camera.startPreview();
}
}