I have written a simple application in which the user can:
press a button to open the camera application
take pictures with the camera application
Is there any way to disable the shutter sound of the camera from my code?
I currently hold an Orange Nivo phone with Android 4.1.2 version on it.
A secion of my code is:
public void onClick(View v) {
try {
f = createImageFile();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath());
Bitmap newphoto = Bitmap.createScaledBitmap(photo, 200, 200, false);
imageView.setImageBitmap(newphoto);
I would appreciate any suggestions on how to achieve this effect.
I know that there are applications on Android store that take pictures without the shutter sound, so i suppose there must be a way to do this without rooting the phone.
The simple answer is: you can not!
The reason is that it is against the law to take a picture without the shutter making sound, this is for privacy concerns. Moreover, as you will notice, you can't take a picture even when your camera preview is not set properly, still for privacy reasons.
At this point you have four options:
Find a way to hack the API
Root the phone and disable the shutter sound
Write your own native code for the camera...but this may be highly dependant on the device you're using it
Use the siplest way that is also the way used by most (if not all) the silent camera
applications in the market. Simply intercept the preview frame via the onPreviewFrame callback from your onClickListener and then save it as an image. The major drawback here is that the maximum preview resolution is far less than the maximum picture resolution so the photo you will take this way will have a fairly low resolution. Indeed if you read the comments on the silent camera apps on the market you will see a lot of people complaining about the resolution of the images not being so high. This is the reason why: they use the trick I exaplained you above.
To conclude, there is no easy way to achieve what you want!
Related
I need a camera that allows me take multiple pictures at once and then select one. Others may or may not be stored on the device. I've tried this. I can take multiple images but how to select one and use it in my app? I read the documentation related to camera2 but its hard to understand without any practical example.I also tried these, but an isolated snippet won't help.
Any example related to the use of burst camera would help.
I do not expect a full code, but any directions on how to proceed? Is it possible to display the picture thumbnails as an when they are clicked on the camera screen itself. I'll need to have the bitmap of the image selected.
I can rephrase any part of the question if not clear.
Try this
you can Call your second startActivityForResult() from the onActivityResult() you get from your first startActivityForResult().
like this by this code you can get 10 pic
public int PIC_CODE=0;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
// get new image here like this
if(PIC_CODE<10){
// add new requset of picture like this
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
PIC_CODE++;
}
}
You have to implement your own camera to take multiple pictures. Create a class with surface view and implements SurfaceView.Callback. Please check out my library which will implement the same.
https://github.com/SripadRaj/BurstCamera
I'm using different tab, almost the sames.
In my app, I want to take picture with the camera with this code
public void takePicture(View v) {
imageFilePath = file_path + "/" + "Photo_" + idFiche + ".png";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(takePictureIntent, REQUEST_CODE_PICTURE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
InputStream stream = null;
if (requestCode == REQUEST_CODE_PICTURE && resultCode == Activity.RESULT_OK) {
//blablabla
}
}
It works well for some tab, but with others, it's impossible to click on the activation button of the photo app to save the picture, as indicated in below picture with the red border.
I think the camera app is wrong, even if I can take picture from the device directly (not from my app), but how to solve it? Can I delete the picture app and use another app with the same code? Or launch a specific app through new intent???
Thank for your help
I think the camera app is wrong
Most likely. Many camera apps seem to go through little testing of ACTION_IMAGE_CAPTURE.
how to solve it?
Stop using ACTION_IMAGE_CAPTURE. Use the camera APIs directly or through a third-party library.
Can I delete the picture app
If by "the picture app", you mean "the camera app", it is likely that the camera app is pre-installed and cannot be uninstalled. Plus, the user may not appreciate you attempting to uninstall their camera app.
and use another app with the same code?
Your code will already give the user a choice of camera apps, if there is more than one that supports ACTION_IMAGE_CAPTURE installed on the device.
Or launch a specific app through new intent?
It is highly unlikely that your desired "specific app" exists on the device.
Followed Android documentation to write code for launching native camera via intent:
http://developer.android.com/training/camera/photobasics.html
Problem: I am using Andorid Native Camera App for taking pictures from my app and launching via intent as mentioned in above link (MediaStore.ACTION_IMAGE_CAPTURE) and camera is launched successfully. When I click camera button of Native app to take pictures, image preview is mirrored while taking selfie (left image appears right) which selfie user would hate as image preview is not what he clicked.
Second - once image is clicked, it shows image preview and waits for user input for acceptance or rejection of picture.
Once image is accepted, OnStartActivityResult (as mentioned in above link) function receives the call and saves image to gallery. Strange thing here is that: Image gets inverted by 180 degree and then saved which is very weird behaviour.
Finally, two problems here: Image Preview Mirroing issue before user approves and while saving image invert issue (180 degree reverse).
Device: Samsung A6 Edge
Android: 5.1
Camera In Manifest File: Android.hardware.camera2 as Camera is deprecated
Please advise how can i fix above two issues.
Also- I have another doubt: Shall i use android native camera app or write the code using Camera Framework and launch custom camera? My requirement just to click pictures and show preview and save it with correct orientation. After searching a lot, I am doubtful - Can native camera app fix these issues? but your expert advise can help on this.
Any quick support and guidance on this is highly appreciated. Thanks in advance !
Here is the code:
public void triggerCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagePath = AppPhotoHelper.getOutputMediaFile();
// Continue only if the File was successfully created
if (imagePath != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(imagePath));
takePictureIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
this.setImagePath(imagePath);
this.startActivityForResult(takePictureIntent, CAMERA_PIC_REQUEST);
}
}
onActivityResult function:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
if (resultCode == RESULT_OK ) {
//AppPhotoHelper is my class to show image in gallery
AppPhotoHelper.displayInGallery(this.getImagePath(), this);
//Image captured and stored in gallery ... camera invoked for next shot after doing some business processing
this.triggerCamera();
}
else if (resultCode == RESULT_CANCELED) {
return;
}
else {
return;
}
}
}
As mentioned in my post, the native camera app differs from device to device. Did you try your code with another device or emulator? I published a library on github that solves many issues, including the image orientation across a wide variety of devices. Feel free to check it out, run the sample code on your device and check if it works properly for your use case.
Simple working code:
m_TakeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (m_TakeVideoIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
startActivityForResult(m_TakeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
// and when done:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_VIDEO_CAPTURE) {
// code
} else {
// other code
}
}
}
Question:
How can I know in onActivityResult(..) what camera was used, front or back ?
OR
How can I force the new Intent(MediaStore.ACTION_VIDEO_CAPTURE) to use the front or back camera ?
(This is because when using the front camera - the result video plays upside down.)
This is a bit long but has useful info about what is actually going wrong when a video plays back in the wrong orientation. Get a nice drink and read on...
Original questions
How can I force the new Intent(MediaStore.ACTION_VIDEO_CAPTURE) to use the front or back camera?
As stated elsewhere, there is an (unreliable) method to ask a camera or video activity to open in either front camera or rear camera mode.
Besides the unreliability of this method, there is nothing about it that will prevent the user from switching front/rear mode after the camera opens, so it will never be a good solution to the problem of upside-down video playback.
How can I know in onActivityResult(..) what camera was used, front or back?
There is no way that I know of. But...
Front vs. back camera is not really the problem anyways.
The underlying problem is that, with some media players, a video captured by an android device may play back in the wrong orientation. This is because, according to the MediaRecorder docs, setting an orientation hint
... will not trigger the source video frame to rotate during video recording, but to add a composition matrix containing the rotation angle in the output video...
And unfortunately:
Some video players may choose to ignore the compostion matrix in a video during playback.
Reading video orientation
Although some media players ignore the orientation hint, your own code can read it.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_VIDEO_CAPTURE) {
Uri vid = data.getData();
Log.i("xcode", "Video captured: " + data.getData());
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(this, vid);
String foo = mmr
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
Log.i("xcode", "Video captured: " + data.getData() + " rotation: "
+ foo);
}
}
(Note that in order to read metadata from the returned video, you will need to provide the MediaStore.EXTRA_OUTPUT field when you request it, as recommended by the Camera docs.)
When I run this code, I get the following results:
Rear camera, portrait, rotation == 90
Front camera, portrait, rotation == 270
Rear camera, landscape, rotation == 180
Front camera, landscape, rotation == 180
It looks like these video captures can be problematic for some players when the rotation is 270, so those are the videos you should select out to be fixed by post-processing.
See https://stackoverflow.com/a/11159760/192373 - it uses an undocumented extra to control which camera will be opened.
Please note that the intent may be served by different apps, depending on the device, ROM, and the third-party apps the end user chose to install and activate.
For image capture, you have a chance to discover the camera info afterwise, but not for video.
I am working on an app in which i have to click a pic a pic and save it to a specified folder. I am using android.provider.MediaStore.ACTION_IMAGE_CAPTURE in intent to invoke the camera .I am done with coding and my activity is working fine.But now i have a question in my mind that whether i should stick with this code or i should use the code given here.Need your precious suggestions on this topic.
Thanx in advance.
If you want to just click a picture and save it to a specified folder nothing more then You can use Intent and call ACTION_IMAGE_CAPTURE, it easy to let handle on camera activity do your stuff,
And If your application has some serious deep work with camera when you want to modify preview screen size, and all those things,(For this you have to handle all things like to manage camera, and when to release it, check for don't freeze main UI..) then you have to go with the code you suggested...
Choice is yours.....
I suggest you use the code from your link.
Because most of the stock camera apps don't work as expected with Image Capture. For example the Galaxy S2 and most other Samsung and HTC phones give you the picture bytes back and also save the picture in the standard DCIM Folder on SD-Card, if you want it or not.
public void imageFromCamera() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d(TAG, "No SDCARD");
} else {
mImageFile = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApp",
"PIC"+System.currentTimeMillis()+".jpg");
mTempImagePath = mImageFile.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mImageFile));
startActivityForResult(intent, TAKE_PICTURE);
}
}
this what u r searching i am thinking..