I created a burst camera and i want that when the activity starts, the camera starts taking pictures automatically without pressing any button.
It says: Unfortunately, FrontVerify has stopped when i try to put:
preview.camera.takePicture(shutterCallback, rawCallback,
jpegCallback);
on the onCreate method.
The thing is: I want to create a series of photos ta simulate a button. When the user put the finger near the camera and it stays all black, i have the algorithm that tells me that BLACK = TRUE, so move on to the next activity. Therefore i don't need any physical or digital button, i could use the camera for that issue.
So the only way so far i made it work so far was with the onClick method and
i really want to get rid of the onClick method that is here:
public void onClick(View v) {
preview.camera.takePicture(shutterCallback, rawCallback,
jpegCallback);
buttonClick.setEnabled(false);
}
And the algorithm for the burst camera is this one:
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
// write to local sandbox file system
// outStream =
// CameraDemo.this.openFileOutput(String.format("%d.jpg",
// System.currentTimeMillis()), 0);
// Or write to sdcard
outStream = new FileOutputStream(String.format(
"/sdcard/eyeverify/still%d.jpg",
System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
try {
stillCount++;
camera.startPreview();
if (stillCount < 10) {
preview.camera.takePicture(shutterCallback, rawCallback,
jpegCallback);
} else {
stillCount = 0;
buttonClick.setEnabled(true);
}
} catch (Exception e) {
Log.d(TAG, "Error starting preview: " + e.toString());
}
}
};
}
So i cant figure it out, how to start the App and the camera start bursting automatically.
Just:
new Handler().postDelayed(new Runnable(){
public void run(){
yourBtn.performClick();
}
}, 1000).
Call this in your OnStart or you OnCreate:
preview.camera.takePicture(shutterCallback, rawCallback,
jpegCallback);
Depending on how you want to manage your app on OnPause and OnResume it may be better to put it into OnResume. If you want it to start with the camera burst whenever paused, or just when first launched.
I would also ensure you clean up your resources in OnStop.
Related
I want to take picture using camera app built in the device with touch event even though device doesn't support that function.
What i want to realize is following.
1) When I open the native or any other camera app,
2) Take a picture with touch event instead of camera button ( This part is what i want to develop)
Below code is What I try for this.
I tried to call transparent Activity on the camera app,
and When I get a touch event on the that Activity,
I call Take_picture() function.
But camera.takePicture() function in the Take_picture doesn't work. ( actually it doesn't call jpegCallback function)
private void Take_picture(){
camera = Camera.open();
if(camera != null)
{
camera.takePicture(null, null, jpegCallback);
}
}
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
new SaveImageTask().execute(data);
}
};
private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
#Override
protected Void doInBackground(byte[]... data) {
FileOutputStream outStream = null;
System.out.println("66666");
// Write to SD Card
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/camtest");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
outStream.write(data[0]);
outStream.flush();
outStream.close();
//Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length + " to " + outFile.getAbsolutePath());
//refreshGallery(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return null;
}
}
I couldn't get any information How to I control native camera app for take picture instantly.
Please help.
How to I control native camera app for take picture instantly.
You can't. You are welcome to create your own camera app that takes pictures however you want. The authors of other camera applications are welcome to implement their camera apps however they want, and they do not have to provide any means for other developers to dictate when and how the pictures are taken.
But camera.takePicture() function in the Take_picture doesn't work
Your app should be crashing, as you should not have a valid Camera object. Only one app can use the camera at a time.
I'm doing an app, that needs the device(usually a tablet) to be in landscape, but the picture has to be shown in portrait in the screen.
Until here, I have done it. Bu now, when I take a picture the "preview" image, is showed in landsacape and looks very strange.
See image to see what I mean:
How you see before take image:
And thats after take picture:
And I don't know how to fix it
Thats surfaceView:
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// stop Preview Before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignored: is trying to stop a non-existent preview
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d("CAMERAPREVIEW", "Error starting camera preview : " + e.getMessage());
}
}
And the method overrided
private PictureCallback mPicture = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
Log.d(TAG, "Error creating media file, check storage permissions: ");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
// Not used for the moment
// Intent returnIntent = new Intent();
// setResult(RESULT_CANCELED, returnIntent);
// finish();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
You can use Camera.setDisplayOrientaion() to rotate the preview on the SurfaceView. But this does not effect the captured image. Your post does not reveal how you display the captured image from file (I assume this is what you show in the second picture). If you load it as a bitmap into an ImageView, you can request rotation for the bitmap.
It also looks like your picture dimensions have different aspect ratio from the preview. I suggest that you look at a recent discussion here.
But maybe I misunderstand what you are doing. Maybe your problem is that you don't restart preview in onPicturetaken()?
I'm writing an application with one of the key features being taking a photo and writing it to a file, then reading that photo into a base64 array (all in the one button click). The problem being that when i initiate the onclick to take a photo it will return from this function before the onPhotoTaken() function has received the image and written it to the storage directory specified.
I have added log outputs at several stages in the code and it is clear that the onclick takePhoto function is exiting before the onPhotoTaken() function that it calls is finished.
The android documentation states that you need to wait for JpegCallback to finish returning before you can restart the preview but I am having trouble getting it to wait for the write to finish.
code:
public static void takePhoto(){
fCamera.takePicture(null, null, jpegCallback);
Log.d("capture", "photo was captured");
// Set the image callback
Log.d("this one is", "being called");
}
static PictureCallback jpegCallback = new Camera.PictureCallback() {
// Function for handling the picture
public void onPictureTaken(byte[] data, Camera fCamera){
//fCamera.stopPreview();
Log.d("is this", "not being called ??? probably");
File imagePath;
FileOutputStream out = null;
// create the filename with extension
String fileName = "IMAGE_1.bmp";
// Create / Find the storage Directory for our pictures
//storageDir = context.getDir("imageDir", Context.MODE_PRIVATE);
// Create it if it doesn't exist
// Create the image file
imagePath = new File(storageDir, fileName);
String finalPath = imagePath.getAbsolutePath();
Log.d("location", finalPath);
if (!imagePath.exists()) {
if (!imagePath.mkdirs())
Log.d("#string/app_name", "Failed to create File");
return;
}
try {
out = new FileOutputStream(imagePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//finalImage.compress(CompressFormat.PNG, 100, out);
try {
out.write(data);
Log.d("write", "photo was written");
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
Log cat:
10-13 12:55:39.185: D/capture(7126): photo was captured
10-13 12:55:39.185: D/this one is(7126): being called
These are the only log outputs that occur.
I have the same problem, by taking a photo and then try to create a thumbnail and store it to the media gallery.
As it's defined in here(developer.android.com/reference/android/hardware/Camera.html#takePicture) takePicture is an asynchronous proccess, so you have to derive your program logic and mandate the sequence.
This can be implemented with AsyncTask (http://developer.android.com/reference/android/os/AsyncTask.html)
In this example, the code inside onPostExecute method will be executed after camera.takePicture (so you are sure that onPictureTaken from PictureCallback is done).
How can i capture a picture from front camera without preview and save it to SD card.
Kindly help me with source code.
public void takePictureNoPreview(Context context){
// open back facing camera by default
Camera myCamera=Camera.open();
if(myCamera!=null){
try{
//set camera parameters if you want to
//...
// here, the unused surface view and holder
SurfaceView dummy=new SurfaceView(context)
myCamera.setPreviewDisplay(dummy.getHolder());
myCamera.startPreview();
myCamera.takePicture(null, null, getJpegCallback()):
}finally{
myCamera.close();
}
}else{
//booo, failed!
}
private PictureCallback getJpegCallback(){
PictureCallback jpeg=new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream fos;
try {
fos = new FileOutputStream("test.jpeg");
fos.write(data);
fos.close();
} catch (IOException e) {
//do something about it
}
}
};
}
}
Not possible, however, there's work-arounds.
See this previous answer, and please, try searching before asking in the future: https://stackoverflow.com/a/3881027/181002
I'm trying to capture an image using Android Camera via simple activity.
Image is clicked and stored. But the problem is, image is either distorted or fragments of older image is concatenated with the currently clicked image. Image is too dark. Here's the CODE : -
public class Cameras extends Activity {
public Camera camera;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
camera=Camera.open();
camera.lock();
Parameters parameters = camera.getParameters();
parameters.setJpegQuality(1);
parameters.setJpegThumbnailQuality(1);
parameters.setJpegThumbnailSize(0,0);
parameters.setSceneMode("night");
parameters.setFocusMode("fixed");
parameters.setPictureSize(640,480);
camera.setParameters(parameters);
camera.takePicture(null,null, jpegCallback);
}
PictureCallback jpegCallback = new PictureCallback() { // <8>
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
// Write to SD Card
outStream = new FileOutputStream(String.format("/sdcard/%d.jpg",System.currentTimeMillis())); // <9>
outStream.write(data);
outStream.close();
camera.unlock();
camera.release();
Toast.makeText(Cameras.this,"Picture Taken",Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally
{
}
}
};
}
Please help regarding this...
I want a neat and clean small size image every time i execute the code.
Thanks... :-)
parameters.setJpegQuality(1);
parameters.setJpegThumbnailQuality(1);
You are requesting very low quality. Try using higher values for quality (like 70)