resuming camera.preview() after photo has been taken - android

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
}

Related

take photo from camera in service

How can Take picture from front camera in service without showing camera on screen.
I have service class
public class PhotoTakingService extends Service {
//Camera variables
//a surface holder
private SurfaceHolder sHolder;
//a variable to control the camera
private Camera mCamera;
//the camera parameters
private Parameters parameters;
boolean mPreviewRunning = false;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onStart(Intent intent,int startId) {
// TODO Auto-generated method stub
super.onStart(intent,startId);
mCamera = Camera.open();
SurfaceView sv = new SurfaceView(getBaseContext());
try {
Camera.Parameters p = mCamera.getParameters();
mCamera.setParameters(p);
mCamera.startPreview();
mCamera.takePicture(null,null,mPictureCallback);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Get a surface
sHolder = sv.getHolder();
//tells Android that this surface will have its data constantly replaced
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData,Camera c) {
Log.e("Callback TAG","Here in jpeg Callback");
if (imageData != null) {
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream("/sdcard/car_final/Image.jpg");
outputStream.write(imageData);
// Removed the finish call you had here
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) try {
outputStream.close();
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
}
}
};
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
and main activity. I want to call from main activity.
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private static final String TAG = MainActivity.class.getSimpleName();
public static SurfaceView mSurfaceView;
public static SurfaceHolder mSurfaceHolder;
public static Camera mCamera;
public static boolean mPreviewRunning;
/** 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.surfaceView);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button btnStart = (Button) findViewById(R.id.button1);
btnStart.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, PhotoTakingService.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
}
});
/* Button btnStop = (Button) findViewById(R.id.StopService);
btnStop.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
stopService(new Intent(CameraRecorder.this, RecorderService.class));
}
});*/
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
When you are in background thread such as service use SurfaceTexture instead of SurfaceHolder. If you are looking for implementation here is a opensource app where I have implemented background video stream and UI video stream both.

Android StartActivityForResult weird behaviour

So, I have an Activity (say, OneActivity) that call another activity for result like this
public class OneActivity extends Activity {
private static final int REQUEST_OTHER = 1;
private ImageView btnStartAction;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstaceState);
setContentView(R.layout.homeView);
// UI Component Initialization.
}
// some code.
public void btnActionHandleClick(View v) {
Intent intent = new Intent(getApplicationContext(), OtherActivity.class);
intent.putExtra("parameter", "someValue");
startActivityForResult(intent, REQUEST_OTHER);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_OTHER) {
// do something.
}
}
}
In the other hand, I have this OtherActivity that has handle for Camera feature and reciently I added Map handling because it was requested. Until the Map featuring request, the OtherActivity worked perfectly with Camera feature.
public class OtherActivity : FragmentActivity {
private static final int REQUEST_CAMERA = 1;
private ImageView btnTakePicture;
private ImageView btnClose;
private ImageView mImageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galeryView);
// UI Component Initialization.
mImageView = (ImageView) findViewById(R.id.mImageView);
}
public void btnTakePictureHandleClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_CAMERA);
}
}
public void btnCloseHandleClick(View v) {
Intent intent = new Intent();
intent.putExtra("returnVale", "returnValue");
if (getParent() == null) {
setResult(Activity.RESULT_OK, intent);
} else {
getParent().setResult(Activity.RESULT_OK, intent);
}
finish();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
}
Like I said before, this code was working perfectly but since I added Map interaction to the application I started to notice some weird behaviour when I take a picture: it start the ACTION_IMAGE_CAPTURE intent but after I take the picture it returns to the onActivityResult from OneActivity class with resultCode = RESULT_CANCELED.
By the way, the Map work fine and have the right interaction :P
Does anyone else has notice some behaviour like this before? Could be the Map featuring that cause this situation ?
Any opinion would be gratefully appreciated.
Thanks in advance!
I would suggest doing your own camera activity. This way you do not have to worry about manufactures different implementations. I got burned on this when Samsung changed their onActivityResult implementation with the Camera.
Here is a camera activity I have created:
public class DTechCameraActivity extends Activity implements OnClickListener {
//declare needed layout variables
private FrameLayout mCameraPreviewLayout;
private ImageView mCaputeredBitmapImageView;
private Button mCaptureImageButton, mRetakeImageButton, mSaveImageButton, mCancelImageButton;
//declare needed variables
private Camera mCamera;
private CameraPreview mPreview;
private String mBitmapSaveFileLocation;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dtech_camera);
//Create an instance of Camera
mCamera = CameraHelper.getCameraInstance();
//Get file location to save
mBitmapSaveFileLocation = getIntent().getExtras().getString(DCGroupActivity.CAMERA_DATA);
//Create and all click listeners for camera buttons
mCaptureImageButton = (Button) findViewById(R.id.camera_button_capture);
mCaptureImageButton.setOnClickListener(this);
mCaputeredBitmapImageView = (ImageView) findViewById(R.id.camera_image_holder);
mRetakeImageButton = (Button) findViewById(R.id.camera_button_retake);
mRetakeImageButton.setOnClickListener(this);
mSaveImageButton = (Button) findViewById(R.id.camera_button_save);
mSaveImageButton.setOnClickListener(this);
mCancelImageButton = (Button) findViewById(R.id.camera_button_cancel);
mCancelImageButton.setOnClickListener(this);
//Create our camera preview view and set as main activity view
mPreview = new CameraPreview(this, mCamera);
mCameraPreviewLayout = (FrameLayout) findViewById(R.id.camera_preview);
mCameraPreviewLayout.addView(mPreview);
showImageCaptureControls();
}
#Override
public void onBackPressed() {
closeDownCameraAndReturnResult(false);
}
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.camera_button_capture:
mCamera.autoFocus(mAutoFocusCallback);
break;
case R.id.camera_button_retake:
showImageCaptureControls();
break;
case R.id.camera_button_save:
closeDownCameraAndReturnResult(true);
break;
case R.id.camera_button_cancel:
closeDownCameraAndReturnResult(false);
break;
}
}
private PictureCallback mPicture = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
showCapturedImageControls(data);
};
};
private AutoFocusCallback mAutoFocusCallback = new AutoFocusCallback() {
#Override
public void onAutoFocus(boolean success, Camera camera) {
mCamera.takePicture(null, null, mPicture);
}
};
private void showCapturedImageControls(byte[] imageBitmapData) {
mCamera.stopPreview();
mCameraPreviewLayout.setVisibility(View.GONE);
mCaptureImageButton.setVisibility(View.GONE);
mCaputeredBitmapImageView.setVisibility(View.VISIBLE);
mRetakeImageButton.setVisibility(View.VISIBLE);
mSaveImageButton.setVisibility(View.VISIBLE);
mCancelImageButton.setVisibility(View.VISIBLE);
saveImageToFile(imageBitmapData);
mCaputeredBitmapImageView.setImageBitmap(ImageManipulator.convertFileToBitmap(mBitmapSaveFileLocation));
}
private void saveImageToFile(byte[] imageBitmapData) {
ImageManipulator.saveInitialBitmap(imageBitmapData, mBitmapSaveFileLocation);
int angle = 90;//ImageManipulator.getTakenImageAngle(mBitmapSaveFileLocation);
if (angle > 0)
ImageManipulator.rotateAndSaveImage(mBitmapSaveFileLocation, angle);
else
ImageManipulator.saveReducedImage(mBitmapSaveFileLocation, BitmapFactory.decodeFile(mBitmapSaveFileLocation), true);
}
private void showImageCaptureControls() {
mCaputeredBitmapImageView.setVisibility(View.GONE);
mRetakeImageButton.setVisibility(View.GONE);
mSaveImageButton.setVisibility(View.GONE);
mCancelImageButton.setVisibility(View.GONE);
mCameraPreviewLayout.setVisibility(View.VISIBLE);
mCaptureImageButton.setVisibility(View.VISIBLE);
mCamera.startPreview();
}
private void closeDownCameraAndReturnResult(boolean actionSaveImage) {
mCamera.release();
if(!actionSaveImage)
setResult(RESULT_CANCELED);
else
setResult(RESULT_OK, new Intent());
this.finish();
}
}
This is how I called it:
mDTech.getSelectedEquipment().setImageDesc(
mEquipmentDescription.getText().toString());
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, getImageUri());
startActivityForResult(cameraIntent, DTechMobileApplication.CAMERA_REQUEST);
Here is my activityResult code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK
&& requestCode == DTechMobileApplication.CAMERA_REQUEST) {
String imageLocation = Environment.getExternalStorageDirectory()
.getAbsolutePath() + getImageLocation();
int angle = ImageManipulator.getTakenImageAngle(imageLocation);
if (angle > 0)
ImageManipulator.rotateAndSaveImage(imageLocation, angle);
else
ImageManipulator.saveReducedImage(imageLocation,
BitmapFactory.decodeFile(imageLocation), true);
saveEquipmentImageToPhone(mDTech.getSelectedEquipment()
.getImageID(), mDTech.getSelectedEquipment()
.getEquipmentID(), imageLocation, mDTech
.getSelectedJob().getJobID(), mDTech
.getAccessingEmployee().getTechID(), mDTech
.getAccessingEmployee().getName(), mDTech
.getSelectedEquipment().getEquipmentNum(), mDTech
.getSelectedEquipment().getDescription());
mTestImageUpload = SHOULD_UPLOAD_IMAGE;
}
}
Here is CameraHelper:
package com.Utilities;
import android.hardware.Camera;
public class CameraHelper {
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
}
And here is CameraPreview:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
//declare needed constants
private final String CLASS_NAME = getClass().getName();
//declare needed variables
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
} catch (IOException e) {
Log.d(CLASS_NAME, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(CLASS_NAME, "Error starting camera preview: " + e.getMessage());
}
}
}
Well, it turns out that one of the views (layouts) should have the #android:noHistory=true flag set but instead of the right layout I put that configuration into the wrong layout. Because of the layout had the noHistory flag set on true, it never returns to onActivityResult but it returns to the previous activity.
So, it wasn't the Google Map Api nor the Camera feature ... it was my mistake :(

Send surface view camera image back to parent activity

I have a surfaceview class used to take a picture with the camera, I want to know how to send back this image taken back to the class that called it. I tried sending the image back through an intent not sure if I implemented this correctly. I have the main class onActivityResult as follows:
//WHAT TO DO WITH RESULT DATA FROM CAMERA
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == CAMERA_PIC_REQUEST && picCount < 5) {
//curPic = (Bitmap) data.getExtras().get("data");
curPic = (Bitmap) data.getExtras().get("files");
ImageView images = new ImageView(getApplicationContext());
//SET PICTURE TO NEW VIEW AND ANIMATE INTO POSITION
images.setImageBitmap(curPic);
//ADD TO PREVPICS LAYOUT
images.setPadding(3, 0, 0, 0);
showCase.addView(images);
badge.setImageBitmap(curPic);
//ADDS CLICK LISTENER TO EACH ELEMENT AND SETS ID
try{
Log.i("AFTER TAKING PIC", "PICCOUNT IS NOW:"+picCount);
showCase.getChildAt(picCount).setId(picCount);
images.setId(picCount);
showCase.getChildAt(picCount).setOnClickListener(btnListener);
images.setTag("pics");
//SAVE PITCTURE
savePic(curPic);
previewImages(picCount);
}catch(Exception e){Log.e("ERROR TAKING PIC", e.toString());}
}else
Toast.makeText(getApplicationContext(), "Unable to add more pictures", Toast.LENGTH_SHORT).show();
}
And the surfaceview class as follows:
public class SecondCamera extends Activity implements SurfaceHolder.Callback {
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_cam);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
/*
* View viewControl = controlInflater.inflate(R.layout.control, null);
* LayoutParams layoutParamsControl = new
* LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
* this.addContentView(viewControl, layoutParamsControl);
*/
Button buttonTakePicture = (Button) findViewById(R.id.button1);
buttonTakePicture.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback, myPictureCallback_RAW,
myPictureCallback_JPG);
}
});
}
ShutterCallback myShutterCallback = new ShutterCallback() {
#Override
public void onShutter() {
}
};
PictureCallback myPictureCallback_RAW = new PictureCallback() {
#Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
}
};
PictureCallback myPictureCallback_JPG = new PictureCallback() {
#Override
public void onPictureTaken(byte[] rawImg, Camera arg1) {
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(rawImg, 0, rawImg.length);
try {
Intent cameraIntent = new Intent();
cameraIntent.putExtra("files", bitmapPicture);
setResult(123, cameraIntent);
finish();
} catch (Exception e) {
Log.e("IMAGE CONVERT", e.toString());
}
}
};
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (previewing) {
camera.stopPreview();
previewing = false;
}
if (camera != null) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
AFAIK, you can't pass a Bitmap through an Intent directly. I imagine you want to save this somewhere, say in local storage. You can save it using File methods. You can pass that file path as a String through the Intent and use that to access it in your previous Activity.
Another option could be to store it in an ArrayList and pass that as a Serializable

two strange problems

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.

Android onPictureTaken not being called

I'm trying to make a screen where the use will be able to take a photo. This photo will go to the database. The problem is, I've tried multiple approaches and in every approach, I need to use the PictureCallback. But the onPictureMethod from PictureCallback is never called, no matter what.
I already tried looking into this and this, but still can't move.
Edit: Using Android 2.2 (API 8)
EDITED CODE:
package touchcare.idealogix.android;
//imports
public class CameraActivityTest extends Activity implements SurfaceHolder.Callback
{
private SurfaceView surfaceCamera;
private SurfaceHolder holderCamera;
private Camera camera;
private Button btnCapture;
private PictureCallback mPictureJpg;
private PictureCallback mPictureRaw;
private ShutterCallback shutterCallback;
#Override public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.layout_camera);
Log.d("PICTURE", "ACTIVITY START");
surfaceCamera = (SurfaceView) findViewById(R.id.surfaceCamera);
btnCapture = (Button) findViewById(R.id.btnCapture);
holderCamera = surfaceCamera.getHolder();
holderCamera.addCallback(this);
holderCamera.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
shutterCallback = new ShutterCallback()
{
#Override
public void onShutter()
{
//DONOTHING
}
};
mPictureRaw = new PictureCallback()
{
#Override
public void onPictureTaken(byte[] data, Camera camera)
{
//DONOTHING
}
};
mPictureJpg = new PictureCallback()
{
#Override
public void onPictureTaken(byte[] data, Camera camera)
{
Log.d("PICTURE", "ON PICTURE TAKEN START");
Intent intent = getIntent();
final boolean beds = intent.getExtras().getBoolean(Main.BEDS, false);
final boolean nurse = intent.getExtras().getBoolean(Main.NURSE, false);
final int position = intent.getExtras().getInt(Main.POSITION, 0);
if(beds)
{
BedroomDataSource bedDs = new BedroomDataSource(CameraActivityTest.this);
String where = SQLiteBedroomHelper.COLUMN_UID + "=" + AppData.getBedrooms().get(position).getUid();
bedDs.open();
bedDs.updateBedroom(where, null, null, null, null, null, null, data);
bedDs.close();
Log.d("PICTURE", "BEDS BOOLEAN");
AppData.getBedrooms().get(position).setImage(data);
}else if(nurse)
{
}
Log.d("PICTURE", "ON PICTURE TAKEN END");
finish();
}
};
btnCapture.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
camera.takePicture(shutterCallback, mPictureRaw, mPictureJpg);
}
});
}
#Override public void surfaceCreated(SurfaceHolder holder)
{
camera = Camera.open();
Camera.Parameters params = camera.getParameters();
params.setPreviewSize(holder.getSurfaceFrame().width(), holder.getSurfaceFrame().height());
params.setPictureSize(32, 32);
params.setJpegQuality(100);
camera.setParameters(params);
try
{
camera.setPreviewDisplay(holder);
}catch(IOException ex){
Log.d("PICTURE", "PREVIEW EXCEPTION");
}
camera.startPreview();
Log.d("PICTURE", "SURFACE CREATED");
}
#Override public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
Log.d("PICTURE", "SURFACE CHANGED");
}
#Override public void surfaceDestroyed(SurfaceHolder holder)
{
camera.stopPreview();
camera.release();
Log.d("PICTURE", "SURFACE DESTROYED");
}
}
Thanks.
EDIT: Looks like that some photos are taken and the callback is reached. The database gets the photos, and loads back in bitmap just fine. But depending on what I'm taking a photo of, the callback isn't reaching. Smaller and simpler things are 100% chance the callback will be executed.
It looks like you create the callback mPicture, and then never set it as the callback for anything. I've never used any picture stuff, so I can't advise on where to set it, but as far as I know you need to register the callback for it to be called, just defining it won't do you any good.

Categories

Resources