Reputation: 9842
I have an app that uses a MediaStore.ACTION_IMAGE_CAPTURE
intent to take a photo, and then returns the photo to an OnActivityResult
function.
However, I'd like to replace this with my own custom camera activity, and integrate it into the app.
Are there any guides out there on doing this? I am new to android and I've never made a camera activity before, or hooked one up to other parts of an app.
Thanks for any help
Upvotes: 0
Views: 2100
Reputation: 1
I hope you have the answer you seek already, but for the others that come across this post - I found a nice tutorial for making your own camera application. Skip to the bottom for the relevant, short answer to this question.
https://www.youtube.com/watch?v=CuvVpsFc77w&list=PL9jCwTXYWjDIHNEGtsRdCTk79I9-95TbJ is what I used to make my camera app after spending DAYS trying to force my file input to accept a rotated image.
Native camera apps gave me a headache(I am also new to Android). My file input caught the image too quickly(through Webview), so I tried making my own camera app to remedy the situation. Make another activity and just input your camera code. When action.image.capture scans for apps, it'll pop up. Mine actually replaced the native camera(but it just shows the MainAppName, not camera specifically..still if you click on it the custom camera opens. Mine did).
Making my own camera app made me realize that it always saves in landscape anyway. I was heartbroken. So for those who want to use a custom camera thinking they need to rotate it there, it's much easier just to fetch the data on Activity result:
Bitmap bitmap = BitmapFactory.decodeFile(mImageFileLocation);
Bitmap rotatedBitmap = rotateImage(bitmap);
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(photoFile);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
private Bitmap rotateImage(Bitmap bitmap) {
ExifInterface exifInterface = null;
try {
exifInterface = new ExifInterface(mImageFileLocation);
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Matrix matrix = new Matrix();
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
default:
}
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return rotatedBitmap;
Note: RotateImage() is not part of OnActivity Result
The above code is not exactly on topic, but I think alot of people want to replace the Media store options because of native camera problems.
Answer: To get your custom camera to pop up: just make another Activity and write your camera code there. Dont't change it from Action Image Capture or it will not return properly(don't be like me).
You can also set the intent directly to start the new camera activity.
Instead of startActivityForResult(chooseBetweenTheseIntent, FILECHOOSER_RESULTCODE);
use:
startActivityForResult(directCustomCameraIntent, FILECHOOSER_RESULTCODE)
Again, don't be like me. startActivityForResult is what you use to get a result back from an activity, don't change it to startActivity.
Side Note: The same YouTube tutorial guy also has a video on rotating the image. You don't need a custom app to rotate, just make a bitmap like the code above, rotate it, and write it back to the original file at the top of your onActivityResult, and whatever grabs the file will have the correct format.
Upvotes: 0
Reputation: 18137
There's a basic guide to this on the Android developer pages:
http://developer.android.com/guide/topics/media/camera.html#custom-camera
In short, you have to build the UI you want for capturing an image; usually you'll want a viewfinder SurfaceView or TextureView, and then some controls.
Then, you'll need to create a Camera object, connect your viewfinder to it, and then start up preview. Once that's going, you can take a picture, change settings such as zoom, and so on.
See the guide for details and example code.
In Android L, there's a whole new Camera2 API, which is more powerful, but for simple use cases the older camera API will work just fine.
Upvotes: 2