I got two strange problems:
1-i'm using previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); in an App and it works normally and I tried to use the same method in another App, I found eclipse cross this method with a black line and says this method is deprecated while I'm using the same method in another App and it works correctly without any problems
JAVA CODE:
public class AugReal00 extends Activity {
SurfaceView cameraPreview;
SurfaceHolder previewHolder;
Camera camera;
Boolean inPreview;
SurfaceHolder.Callback surfaceCallback = new Callback() {
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
//Open The Camera
this.camera = android.hardware.Camera.open();
this.camera.setPreviewDisplay(this.holder);
}
catch(IOException ioe) {
ioe.printStackTrace(System.out);
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
android.hardware.Camera.Parameters parameters = camera.getParameters();
parameters.setSceneMode(parameters.SCENE_MODE_SPORTS);
parameters.setFlashMode(parameters.FLASH_MODE_ON);
parameters.setPreviewSize(width/2, height/2);
parameters.setPictureSize(width/2, height/2);
camera.setParameters(parameters);
camera.startPreview();
}
};// end of surfaceCallback Listener
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aug_real00);
inPreview = false;
cameraPreview = (SurfaceView) findViewById(R.id.cameraPreview);
previewHolder = cameraPreview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//previewHolder.se;
}
public void OnResume() {
super.onResume();
camera = Camera.open();
}// end of OnResume
public void OnPause() {
if (inPreview) {
camera.stopPreview();
}
camera.release();
camera=null;
inPreview=false;
super.onPause();
}// end of OnPause.
2-I wrote a simple program and i found that eclipse gives me the following error
syntax error, insert } to complete class body
while all the brackets are closed and every thing should work fine. I don't know how to solve this issue.
Check your old app sdk version number. The same api can be deprecated in next versions.
You have not closed an opened brace. Make sure all the braces are properly inserted.
Related
public class PhotoActivity extends Activity implements SurfaceHolder.Callback {
private ImageView imageView;
private SurfaceView mSurfaceView;
private Bitmap bmp;
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
private Parameters parameters;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
imageView = (ImageView) findViewById(R.id.image_view);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
parameters = mCamera.getParameters();
mCamera.setParameters(parameters);
mCamera.startPreview();
Camera.PictureCallback mCall = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bmp);
mSurfaceView.setVisibility(View.INVISIBLE);
// Call web service
SendPhotoAsync photoToWeb = new SendPhotoAsync(data);
photoToWeb.execute();
}
};
mCamera.takePicture(null, null, mCall);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
}
how i could modify this code to detect face on camera preview,using face detector api in android.. any help please..
i also want to detect eyes then..hope someone can help me..i went through thiswhich works on button click.in my case for taking photo no button click is there..
someone with some productive code will be appreciable..please help me its very critical
if you have the android API for face detector..call this activity from the photo activity..in photo activity save the captured image to sd card...read this file in the face detector api..thus we can make it..but samsung galaxy+google nexus doesnt perform face detection..AAkash does..
I successfully created a camera surface view, and it works when the app is started/restarted. But it just shows a black screen, after I pause the game and resume it (pressing home screen). Am I missing something here?
Here's the code for the surface (which is created from a Main Activity:
public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
static Camera camera = null;
Surface camSurface;
String TAG = "CameraView";
static SurfaceHolder mHolder;
public CameraView(Context context) {
super(context);
getHolder().addCallback(this);
getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// TODO Auto-generated constructor stub
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Log.e(TAG, "Camera view created");
mHolder = getHolder();
if(camera == null){
camera = Camera.open();
}
if(camera == null){
Log.e(TAG, "Null camera");
}
try {
camera.setPreviewDisplay(getHolder());
} catch (IOException e) {
// TODO Auto-generated catch block
camera.release();
camera = null;
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
synchronized(mHolder){
if(camera!=null){
camera.stopPreview();
camera.release();
camera = null;
}
}
}
}
When you leave and return to your app, you still need to call camera.startPreview() at some point. I would have this in my surfaceChanged() method.
Make sure you have a valid instance of your camera as when you first start it up.
How can I modify live camera view on Android?
I was searching and found this app https://play.google.com/store/apps/details?id=com.fingersoft.cartooncamera as good example what I want to do. I wanted to apply some effect to my camera view as well. I need to find a way to add some effects or draw something on my camera view.
As far as I understood I have to get some video stream, apply some changes and show back in camera view.
Any ideas?
Much thanks!
You can find more information about this topic Android - Camera Preview and in this post.
A completely other approach would be to use OpenCV.
The smallest piece of code I could think of to get an image and preview it on the screen looks like this:
public class MainActivity extends Activity implements Callback {
Camera camera;
SurfaceView surfaceView;
SurfaceHolder holder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
}
#Override
protected void onResume() {
super.onResume();
camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(ImageFormat.JPEG);
camera.setParameters(parameters);
holder = surfaceView.getHolder();
holder.addCallback(this);
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
// You could apply some pixel operations directly here.
Log.d("Camera", "Camera image aquired");
}
});
}
#Override
protected void onPause() {
super.onPause();
camera.stopPreview();
camera.release();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
A full example that does this, can be found on GitHub.
I have a problem with a flashlight app Android 4.0.4 Nexus S i9020.
I have tried dozens of suggestions posted here on stackoverflow but nothing worked for me.
The app worked with Android version 2.3.6 but since 4.0.4 the torch has stopped working.
Here is my impl and the logcat output.
#Override
protected void onResume()
{
super.onResume();
_Camera = Camera.open();
}
#Override
protected void onPause()
{
if (_Camera != null)
{
_Camera.release();
}
}
//called within runnable and post to a handler
private void processOffClick()
{
if (_Camera != null)
{
Parameters params = _Camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
_Camera.setParameters(params);
_Camera.stopPreview();
}
}
//called within runnable and post to a handler
private void processOnClick()
{
if (_Camera != null)
{
Parameters params = _Camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
_Camera.setParameters(params);
_Camera.startPreview();
}
}
I have also tried to execute the onclick offclick methods without runnables.
In Logcat the folling error occurs after onclick is performed.
04-07 14:10:02.719: E/CameraHardwareSec(82): preview window is NULL!
04-07 14:10:02.719: I/CameraHardwareSec(82): virtual android::status_t android::CameraHardwareSec::startPreview() : deferring
There are some camera apps in the market which work with my phone. So there must be some way to get the flashlight on.
I also tried to add an SurfaceView/Holder but it did not work. Maybe I did something wrong.
Cheers Karim
you are doing everything ok, in version 4.0+ you need a surface view. Declare one in your layout.xml
then do something like this:
implement SurfaceHolder.Callback
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init your layout
this._surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
this._surfaceHolder = this._surfaceView.getHolder();
this._surfaceHolder.addCallback(this);
this._surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
protected void onResume()
{
super.onResume();
_Camera = Camera.open();
this._camera.startPreview();
this._camera.setPreviewDisplay(this._surfaceHolder);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
//this._surfaceHolder = holder;
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
//this._surfaceHolder = null;
}
I have a surfaceview which is displaying a camera preview I have it taking a picture and display it on the surfaceview but when I press the back button it closes the application but I want it to display the original camera preview.
I also only want to reset the display when the picture is being displayed and have normally functionality returned to back button when camera preview is showing.
public class cameraView extends Activity implements SurfaceHolder.Callback{
private SurfaceView preview=null;
private SurfaceHolder previewHolder=null;
private Camera camera=null;
private ImageButton bt = null;
private Toast t = null;
private Camera.Parameters param = null;
private Button b = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cam);
t = Toast.makeText(this, "Just Click The Magnifying Glass To Search", 5000);//creates a new pop up message that lasts for 5 seconds
t.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
t.show();
b = (Button)findViewById(R.id.test);
b.setOnClickListener(search);
bt = (ImageButton)findViewById(R.id.button);//creates instance of button
bt.setOnClickListener(search);//starts an on click listener for button
preview=(SurfaceView)findViewById(R.id.myview);//creates instance of surfaceview
previewHolder=preview.getHolder();//creates a surfaceholder
previewHolder.addCallback(this);//sets surfaceholder callback as the activity
previewHolder.setType(3);//sets the type to SURFACE_TYPE_PUSH_BUFFERS
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { //creates a method that is called automatically when the surface is changed
// TODO Auto-generated method stub
Camera.Parameters param = camera.getParameters();//sets param to be equal to camera parametors
param.setPreviewSize(width, height);//sets width and height to that of what is passed back to it when callback calls it
//param.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(param);//sets the camera parameters to param
camera.startPreview();//starts the preview
camera.autoFocus(cb);//calls autofocus callback method
}
public void surfaceCreated(SurfaceHolder holder) {//called when the surface has been created
// TODO Auto-generated method stub
camera = Camera.open();//opens the camera and sets it to the camera variable
try{
camera.setPreviewDisplay(previewHolder);//sets the display area to previewHolder
}catch(Throwable t){
Log.e(""+t, null);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {//called when sureface is destroyed or when activity is closed
// TODO Auto-generated method stub
camera.stopPreview();//stops the preview
camera.release();//releases the camera
camera = null;// clears the camera so it contains no information
}
AutoFocusCallback cb = new AutoFocusCallback(){
public void onAutoFocus(boolean success, Camera camera) {
// TODO Auto-generated method stub
//return true;
}
};
private OnClickListener search = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
switch(v.getId()){
case R.id.test:
param = camera.getParameters();
if(param.getFlashMode().equals(Parameters.FLASH_MODE_TORCH)){
/*
* IF statement to check the current flash mode and change it appropriately
*/
param.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(param);
}else{
param.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(param);
}
break;
case R.id.button:
camera.autoFocus(cb);//calls autofocus with call back of cb
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
camera.takePicture(null, mPictureCallback, mPictureCallback);
}
}, 2000);
break;
}
}
};
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
bt.setVisibility(4);
b.setVisibility(4);
b.setEnabled(false);
bt.setEnabled(false);
}
};
above is my entire code (minus the imports) of my activity.
any help is greatly appreciated.
What you need to do is override the onBackPressed() method:
#Override public void onBackPressed()
{
// Restart camera
}