Android Camera Image Quality Very Low - android

I am trying to capture an image from camera but the image quality is really bad. I don't know why as I am a newbie to coding for android.
I have tried a couple of things but didn't worked out. Here is my code. Please help me where to change the code.
public class MainActivity extends Activity implements TextureView.SurfaceTextureListener {
private Camera mCamera;
private TextureView mTextureView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextureView1 = (TextureView) findViewById(R.id.textureView1);
mTextureView1.setSurfaceTextureListener(this);
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
try {
mCamera = Camera.open(getCameraId());
mCamera.setPreviewTexture(surface);
Camera.Parameters params=mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();
Camera.Size size = sizes.get(0);
for(int i=0;i<sizes.size();i++)
{
if(sizes.get(i).width > size.width)
size = sizes.get(i);
}
params.setPictureSize(size.width, size.height);
params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
params.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);
params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
params.setExposureCompensation(0);
params.setPictureFormat(ImageFormat.JPEG);
params.setJpegQuality(100);
params.setRotation(90);
params.setJpegQuality(100);
params.setPreviewSize(size.width, size.height);
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(getCameraId(), cameraInfo);
setCameraDisplayOrientation(this, getCameraId(), mCamera);
mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
private int getCameraId() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
// Log.d(DEBUG_TAG, "Camera found");
cameraId = i;
break;
}
}
return cameraId;
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
try {
mCamera.stopPreview();
mCamera.release();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
Camera.PictureCallback mPicture = new Camera.PictureCallback(){
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
private static File getOutputMediaFile(int mediaTypeImage) {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
public void onClick(View v){
mCamera.takePicture(null, null, mPicture);
}
}
Which is way lower than 13 megapixel camera in my phone.

Related

How to get rid of camera freezes (SurfaceView)?

I'm trying to create custom camera using Camera API. I have already looked through a lot of similar questions, but anyway can't get rid from freezes in my Camera Preview. Sometimes preview freezes when activity started, despite of using another thread. But when I try to switch to face camera, preview image Freezes every time. In log i Got only something like this:
I/Choreographer: Skipped 41 frames! The application may be doing too much work on its main thread.
My SurfaceView is placed in Fragment in ViewPager activity if it's matter.
My Custom Camera class methods:
Set Display Orientation:
void setCameraDisplayOrientation(int cameraId) {
int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result = 0;
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
result = ((360 - degrees) + info.orientation);
} else
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = ((360 - degrees) - info.orientation);
result += 360;
}
result = result % 360;
camera.setDisplayOrientation(result);
}
Holder Callback class:
class HolderCallback implements SurfaceHolder.Callback {
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if (mIsPreviewing) {
camera.stopPreview();
mIsPreviewing = false;
}
if (camera != null) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size bestSize = getBestPreviewSize(width, height, parameters);
if (bestSize != null) {
parameters.setPreviewSize(bestSize.width, bestSize.height);
camera.setParameters(parameters);
Toast.makeText(
getActivity().getApplicationContext(),
"Оптимальный размер: " + String.valueOf(bestSize.width)
+ " : " + String.valueOf(bestSize.height),
Toast.LENGTH_LONG).show();
}
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
mIsPreviewing = true;
// camera.autoFocus(autoFocusCallback);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private Camera.Size getBestPreviewSize(int width, int height,
Camera.Parameters parameters) {
Camera.Size bestSize = null;
List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();
bestSize = sizeList.get(0);
for (int i = 1; i < sizeList.size(); i++) {
if ((sizeList.get(i).width * sizeList.get(i).height) > (bestSize.width * bestSize.height)) {
bestSize = sizeList.get(i);
}
}
return bestSize;
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
Camera Handler Thread:
private CameraHandlerThread mThread = null;
private static class CameraHandlerThread extends HandlerThread {
Handler mHandler = null;
CameraHandlerThread() {
super("CameraHandlerThread");
start();
mHandler = new Handler(getLooper());
}
synchronized void notifyCameraOpened() {
notify();
}
void openCamera() {
mHandler.post(new Runnable() {
#Override
public void run() {
camera = Camera.open(CAMERA_ID);
//set camera to continually auto-focus
Camera.Parameters params = camera.getParameters();
//*EDIT*//params.setFocusMode("continuous-picture");
//It is better to use defined constraints as opposed to String, thanks to AbdelHady
// params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
// camera.setParameters(params);
//STEP #1: Get rotation degrees
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break; //Natural orientation
case Surface.ROTATION_90: degrees = 90; break; //Landscape left
case Surface.ROTATION_180: degrees = 180; break;//Upside down
case Surface.ROTATION_270: degrees = 270; break;//Landscape right
}
int rotate = (info.orientation - degrees + 360) % 360;
//STEP #2: Set the 'rotation' parameter
params.setRotation(rotate);
camera.setParameters(params);
notifyCameraOpened();
camera.startPreview();
}
});
try {
wait();
}
catch (InterruptedException e) {
Log.d(TAG, "openCamera: Cannot open Camera");
}
}
}
Opening camera:
private void newOpenCamera() {
mThread = new CameraHandlerThread();
synchronized (mThread) {
mThread.openCamera();
}
}
Fragment methods:
#Override
public void onResume() {
super.onResume();
newOpenCamera();
setCameraDisplayOrientation(CAMERA_ID);
}
#Override
public void onPause() {
super.onPause();
if (camera != null)
camera.release();
camera = null;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
sv = (SurfaceView) getActivity().findViewById(R.id.surfaceView);
makePhotoBtn = (ImageView) getActivity().findViewById(R.id.makephotoBtn);
switchCameraBtn = (ImageView) getActivity().findViewById(R.id.switchCameraBtn);
holder = sv.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holderCallback = new HolderCallback();
holder.addCallback(holderCallback);
rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
makePhotoBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
camera.takePicture(null, null, new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
new SaveBitmap().execute(toObjects(data));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
switchCameraBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
swapCamera();
}
});
}
And Swap Camera Method:
private void swapCamera() {
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
holder.removeCallback(holderCallback);
holder = null;
sv = null;
sv = (SurfaceView) getActivity().findViewById(R.id.surfaceView);
}
//swap the id of the camera to be used
if (CAMERA_ID == Camera.CameraInfo.CAMERA_FACING_FRONT) {
CAMERA_ID = 0;
} else {
CAMERA_ID = 1;
}
newOpenCamera();
}
What can I do to get rid of freezes in this case? Appreciate any help!

Picture taken from camera displayed in ImageView is always horizontal Android

I am using the following code to take a picture and then display it on screen to the user using an ImageView. I want the picture to be displayed fullscreen (like on snapchat). However, the picture is always displayed like this horizontally with a white screen everywhere else:
This is my layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="#+id/picturedisplay"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgClose"
android:layout_gravity="right|bottom"
android:text="Flip Cam"
android:padding="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/snap"
android:text="Capture"
android:layout_gravity="center|bottom"
android:padding="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flash"
android:id="#+id/imgOpen"
android:layout_gravity="left|bottom"
android:padding="20dp"/>
and the code:
public class CameraScreen extends Activity {
private Camera mCamera = null;
private SessionManager session;
private String rand_img;
private ImageView preview_pic;
private Bitmap bitmap;
private CameraPreview mCameraView = null;
private byte[] photo;
static final int CAM_REQUEST = 1;
private RandomString randomString = new RandomString(10);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_screen);
session = new SessionManager(getApplicationContext());
try {
mCamera = Camera.open();//you can use open(int) to use different cameras
} catch (Exception e) {
Log.d("ERROR", "Failed to get camera: " + e.getMessage());
}
if (mCamera != null) {
mCameraView = new CameraPreview(this, mCamera);//create a SurfaceView to show camera data
FrameLayout camera_view = (FrameLayout) findViewById(R.id.camera_view);
camera_view.addView(mCameraView);//add the SurfaceView to the layout
}
//btn to close the application
Button imgClose = (Button) findViewById(R.id.imgClose);
imgClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.exit(0);
}
});
//btn to close the application
Button logout = (Button) findViewById(R.id.imgOpen);
logout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
session.logOut();
Intent a = new Intent(CameraScreen.this, MainActivity.class);
startActivity(a);
finish();
}
});
Button snap = (Button) findViewById(R.id.snap);
snap.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
}
#Override
protected void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCameraView.getHolder().removeCallback(mCameraView);
mCamera.release();
}
}
#Override
public void onResume() {
super.onResume();
// Get the Camera instance as the activity achieves full user focus
if (mCamera == null) {
initializeCamera(); // Local method to handle camera initialization
}
}
protected void initializeCamera(){
// Get an instance of Camera Object
try{
mCamera = Camera.open();//you can use open(int) to use different cameras
} catch (Exception e){
Log.d("ERROR", "Failed to get camera: " + e.getMessage());
}
if(mCamera != null) {
mCameraView = new CameraPreview(this, mCamera);//create a SurfaceView to show camera data
FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
camera_view.addView(mCameraView);//add the SurfaceView to the layout
}
}
private File getFile() {
File sdCard = Environment.getExternalStorageDirectory();
File folder = new File(sdCard.getAbsolutePath() +"/city_life_pic");
if (!folder.exists()) {
folder.mkdir();
}
rand_img = randomString.nextString() + ".jpg";
File image = new File(folder,rand_img);
return image;
}
Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
Log.d("ON SHUTTER", "onShutter'd");
}
};
Camera.PictureCallback rawCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("ON PICTURE RAW", "onPictureTaken - raw");
}
};
Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
photo = data;
new SaveImageTask().execute(data);
Log.d("ON PICTURE JPEG", "onPictureTaken - jpeg");
}
};
private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
#Override
protected Void doInBackground(byte[]... data) {
FileOutputStream outStream = null;
// Write to SD Card
try {
bitmap = decodeByteArray(photo, 0, photo.length);
File outFile = getFile();
outStream = new FileOutputStream(outFile);
outStream.write(data[0]);
ExifInterface exif=new ExifInterface(outFile.toString());
Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){
bitmap= rotate(bitmap, 90);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
bitmap= rotate(bitmap, 270);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
bitmap= rotate(bitmap, 180);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")){
bitmap= rotate(bitmap, 90);
}
boolean bo = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
preview_pic = (ImageView) findViewById(R.id.picturedisplay);
camera_view.setVisibility(View.GONE);
preview_pic.setVisibility(View.VISIBLE);
preview_pic.setImageBitmap(bitmap);
}
}
public static Bitmap rotate(Bitmap bitmap, int degree) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
// mtx.postRotate(degree);
mtx.setRotate(degree);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
}
EDIT ROTATION CODE:
switch (rotation)
{
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
if (currentCamInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
//switch camera to back camera
mCamera = Camera.open(camBackId);
result = (currentCamInfo.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else {
//switch camera to front camera
mCamera = Camera.open(camFrontId);
result = (currentCamInfo.orientation - degrees + 360) % 360;
}
if (mCamera != null) {
mCamera.setDisplayOrientation(result);
//rotate camera
mCameraView = new CameraPreview(this, mCamera);//create a SurfaceView to show camera data
camera_view.addView(mCameraView);//add the SurfaceView to the layout
Camera.Parameters p = mCamera.getParameters();
p.setRotation(90);
mCamera.setParameters(p);
}
Try adding mCamera.setDisplayOrientation(90) to your initializeCamera() method. You can also calculate the right orientation settings based on this code.
public static int getCameraDisplayOrientation(int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = Session.currentActivity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
return result;
}

Why can I not open camera in Android?

I'm a beginner in android and I want to open the camera and capture picture automatically.
My Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++ ){
Camera.getCameraInfo(camIdx, cameraInfo);
if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ){
try {
cam = Camera.open( camIdx );
} catch (RuntimeException e) {
Log.i("Camera failed to open: ", e.getLocalizedMessage());
}
}
}
SurfaceView surface = new SurfaceView(this);
try {
cam.setPreviewDisplay(surface.getHolder());
}catch(IOException e){
e.printStackTrace();
}
}
But when code is run, camera intent does not start and shows me black page,and doesn't take a picture automatically. How can I solve that?thanks.
File cDir = getExternalFilesDir(null);
cDir.mkdir();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String mPhotoresultingFile = new File(cDir.toString()+ "/image.jpg");
Uri uriSavedImage = Uri.fromFile(mPhotoresultingFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,uriSavedImage);
startActivityForResult(cameraIntent, 1888);
add onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
if (mPhotoresultingFile.exists()) {
Bitmap photo = BitmapFactory.decodeFile(mPhotoresultingFile
.getPath());
ExifInterface exif = new ExifInterface(
mPhotoresultingFile.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 1);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
} else if (orientation == 3) {
matrix.postRotate(180);
} else if (orientation == 8) {
matrix.postRotate(270);
}
photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(),
photo.getHeight(), matrix, true);
//If you want to delete the captured photo from the gallery
mPhotoresultingFile.delete();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
And add Camers Uses Permission in your manifest file.
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
Make sure you have defined the following permission:
<uses-permission android:name="android.permission.CAMERA" />
and also:
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
defined in your AndroidManifest.xml
you can try to open back camera by changing your code to, cam = Camera.open( 0 );
Use below complete code for your project, code include pinch zoom camera as well as rotation features !
public class RecordLiveVideo extends Activity implements SurfaceHolder.Callback
{
private Camera camera;
private SurfaceView surfaceView;
Button btn_record, btn_rotate;
TextView tv_timer;
MediaRecorder mediaRecorder;
int temp_for_start_stop_button = 0;
long start_time;
static Boolean has_front_camera;
int current_camera = 0;
int recording_degree;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.recordlivevideo);
InitCamera(0);
btn_record = (Button) findViewById(R.id.btn_record);
tv_timer = (TextView) findViewById(R.id.tv_timer);
tv_timer.setVisibility(View.GONE);
btn_rotate = (Button) findViewById(R.id.btn_rotate);
int n = Camera.getNumberOfCameras();
if (n > 1)
{
has_front_camera = true;
}
else
{
has_front_camera = false;
btn_rotate.setBackground(getResources().getDrawable(R.drawable.rotation_close));
}
btn_rotate.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (has_front_camera == true)
{
camera.stopPreview();
camera.release();
camera = null;
if (current_camera == 0)
{
//start front
//recording_degree = 270;
current_camera = 1;
InitCamera(1);
}
else
{
//start back
//recording_degree = degree;
current_camera = 0;
InitCamera(0);
}
}
else
{
Toast.makeText(getApplicationContext(), "Device has not front camera !", Toast.LENGTH_SHORT).show();
}
}
});
btn_record.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (temp_for_start_stop_button == 0)
{
if (startRecording())
{
tv_timer.setVisibility(View.VISIBLE);
tv_timer.setText("0 : 0");
temp_for_start_stop_button = 1;
btn_record.setBackground(getResources().getDrawable(R.drawable.stop_camera));
start_time = System.currentTimeMillis();
show_current_time();
}
}
else
{
tv_timer.setVisibility(View.GONE);
temp_for_start_stop_button = 0;
btn_record.setBackground(getResources().getDrawable(R.drawable.camera));
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(new File(uniqueOutFile));
mediaScanIntent.setData(contentUri);
RecordLiveVideo.this.sendBroadcast(mediaScanIntent);
}
else
{
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
});
}
void InitCamera(int n)
{
surfaceView = (SurfaceView) findViewById(R.id.preview);
surfaceView.getHolder().addCallback(this);
try
{
camera = Camera.open(n);
Camera.Parameters parametro = camera.getParameters();
parametro.setColorEffect(Camera.Parameters.EFFECT_MONO);
parametro.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
camera.setParameters(parametro);
setCameraDisplayOrientation(RecordLiveVideo.this, n, camera);
camera.setPreviewDisplay(surfaceView.getHolder());
camera.startPreview();
}
catch (final Exception e)
{
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
camera.release();
camera = null;
}
}
void show_current_time()
{
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
long duration = (int) ((System.currentTimeMillis() - start_time) / 1000);
int min = 00;
int sec = 00;
min = (int) duration / 60;
sec = (int) (duration % 60);
//Log.e("MAYUR", "" + min + " : " + sec);
if (temp_for_start_stop_button == 1)
{
tv_timer.setText("" + min + " : " + sec);
show_current_time();
}
}
}, 1000);
}
int degree;
//int result;
public void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera)
{
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation)
{
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
{
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270)
degree = result;
else
degree = result + 180;
}
else
{ // back-facing
result = (info.orientation - degrees + 360) % 360;
degree = result;
}
camera.setDisplayOrientation(result);
}
#Override
protected void onDestroy()
{
super.onDestroy();
if (camera != null)
{
camera.release();
}
}
#Override
protected void onPause()
{
super.onPause();
if (camera != null)
{
try
{
camera.stopPreview();
}
catch (Exception e)
{
// TODO: handle exception
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
camera.setPreviewDisplay(holder);
//camera.startPreview();
}
catch (IOException e)
{
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
if (holder.getSurface() == null)
{
// preview surface does not exist
return;
}
if (holder.getSurface() != null)
{
try
{
camera.stopPreview();
}
catch (Exception e)
{
}
try
{
camera.setPreviewDisplay(holder);
camera.startPreview();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder)
{
}
String uniqueOutFile = null;
public boolean startRecording()
{
try
{
if (degree == 90 || degree == 270)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//camera.setDisplayOrientation(degree);
camera.unlock();
mediaRecorder = new MediaRecorder();
mediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener()
{
#Override
public void onError(MediaRecorder mr, int what, int extra)
{
Log.i("mayur", "Error");
}
});
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
uniqueOutFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/VideoEffect/VideoEffect_Camera_" + System.currentTimeMillis() + ".mp4";
File outFile = new File(uniqueOutFile);
if (outFile.exists())
{
outFile.delete();
}
mediaRecorder.setOutputFile(uniqueOutFile);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setVideoEncodingBitRate(3000000);
mediaRecorder.setVideoSize(640, 480);
//mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
mediaRecorder.setOrientationHint(degree);
mediaRecorder.prepare();
mediaRecorder.start();
return true;
}
catch (Exception e)
{
camera.lock();
return false;
}
}
private float mDist;
#Override
public boolean onTouchEvent(MotionEvent event)
{
// Get the pointer ID
Camera.Parameters params = camera.getParameters();
int action = event.getAction();
if (event.getPointerCount() > 1)
{
// handle multi-touch events
if (action == MotionEvent.ACTION_POINTER_DOWN)
{
mDist = getFingerSpacing(event);
}
else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported())
{
camera.cancelAutoFocus();
handleZoom(event, params);
}
}
else
{
// handle single touch events
if (action == MotionEvent.ACTION_UP)
{
handleFocus(event, params);
}
}
return true;
}
private void handleZoom(MotionEvent event, Camera.Parameters params)
{
int maxZoom = params.getMaxZoom();
int zoom = params.getZoom();
float newDist = getFingerSpacing(event);
if (newDist > mDist)
{
//zoom in
if (zoom < maxZoom - 1)
zoom += 2;
}
else if (newDist < mDist)
{
//zoom out
if (zoom > 1)
zoom -= 2;
}
mDist = newDist;
params.setZoom(zoom);
camera.setParameters(params);
}
public void handleFocus(MotionEvent event, Camera.Parameters params)
{
//int pointerId = event.getPointerId(0);
//int pointerIndex = event.findPointerIndex(pointerId);
// Get the pointer's current position
// float x = event.getX(pointerIndex);
// float y = event.getY(pointerIndex);
List<String> supportedFocusModes = params.getSupportedFocusModes();
if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
{
camera.autoFocus(new Camera.AutoFocusCallback()
{
#Override
public void onAutoFocus(boolean b, Camera camera)
{
// currently set to auto-focus on single touch
}
});
}
}
/** Determine the space between the first two fingers */
private float getFingerSpacing(MotionEvent event)
{
// ...
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
#Override
public void onBackPressed()
{
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
}
Enjoy !!

Camera preview is in portrait mode but image captured is rotated

I am trying to capture a photo using the camera. The preview by default was in landscape mode which i could change it to portrait mode using
setCameraDisplayOrientation(this,1, mCamera);
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
The image captured is stored under a folder myImages. But the images is rotated. (look's like the image is captured in landscape mode)
So how can i rotate the image captured and save the same?
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 1;
ImageView imageView;
Button b;
private Camera mCamera;
private CameraPreview mPreview;
private Bitmap bitmap;
private PictureCallback mPicture;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean check =checkCameraHardware(MainActivity.this);
if(check)
{
mCamera = getCameraInstance();
// mCamera.setDisplayOrientation(90);
setCameraDisplayOrientation(this,
1, mCamera);//requires min sdk 9
}
// Create an instance of Camera
mPicture = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
if(!imagesFolder.exists())
imagesFolder.mkdirs();
File pictureFile = new File(imagesFolder, "image.jpg");
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
System.out.println("hello");
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d("No File", "File not found: " + e.getMessage());
} catch (IOException e) {
//Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
b = (Button) findViewById(R.id.button_capture);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCamera.takePicture(null, null, mPicture);
Toast.makeText(MainActivity.this, "Called",1000).show();
}
});
}
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
Toast.makeText(this, "Phone has camera", Toast.LENGTH_LONG).show();
return true;
} else {
// no camera on this device
Toast.makeText(this, "Phone has no camera", Toast.LENGTH_LONG).show();
return false;
}
}
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
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mCamera.release();
}
}
The CameraPreview class is the same from the developer site http://developer.android.com/guide/topics/media/camera.html
Note: I am using the back camera not the front facing camera.
I faced the same problem when taking photo from camera in portrait mode. Below lines of code solved my problem:
public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
Call setCameraDisplayOrientation() method in surfaceCreated callback as the following:
#Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
setCameraDisplayOrientation(getActivity(), CameraInfo.CAMERA_FACING_BACK, camera);
}
I had to rotate the image in Camera onPictureTaken() callback:
camera.takePicture(null, null, new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
if (data != null) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int screenHeight = getResources().getDisplayMetrics().heightPixels;
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// Notice that width and height are reversed
Bitmap scaled = Bitmap.createScaledBitmap(bm, screenHeight, screenWidth, true);
int w = scaled.getWidth();
int h = scaled.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
bm = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, true);
}else{// LANDSCAPE MODE
//No need to reverse width and height
Bitmap scaled = Bitmap.createScaledBitmap(bm, screenWidth,screenHeight , true);
bm=scaled;
}
photoPreview.setImageBitmap(bm);
}
isImageCaptured = true;
photoPreview.setVisibility(View.VISIBLE);
surfaceView.setVisibility(View.GONE);
}
});
Below code worked for me for Front facing camera.
if (data != null) {
try {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int screenHeight = getResources().getDisplayMetrics().heightPixels;
bm = BitmapFactory.decodeByteArray(data, 0,
(data != null) ? data.length : 0);
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(cameraFace, info);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// Notice that width and height are reversed
// Bitmap scaled = Bitmap.createScaledBitmap(bm,
// screenHeight, screenWidth, true);
// int w = scaled.getWidth();
// int h = scaled.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
if (cameraFace == CameraInfo.CAMERA_FACING_FRONT)
mtx.postRotate(180);
// Rotating Bitmap
bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), mtx, true);
} else {// LANDSCAPE MODE
Bitmap scaled = Bitmap.createScaledBitmap(bm,
screenWidth, screenHeight, true);
bm = scaled;
}
} catch (Exception e) {
} catch (Error e) {
}
}
Yes, I tried the way in the answer, it works for back camera, for front camera, it need to rotate 270 not 90. :)

android custom camera orientation issue [duplicate]

This question already has answers here:
Camera preview is in portrait mode but image captured is rotated
(3 answers)
Closed 6 years ago.
I have defined a custom camera view to take picture.
The issue i am getting is if the picture is taken with camera held in "portrait" , the image is 90 rotated. If i take the picture in "landscape" mode, it is getting correct .
My code is below . I tried few solutions like Android - Camera preview is sideways
but it didnt fix my problem.
Please give me some directions.
Thanks.
public class Customcamera extends Activity implements OnClickListener {
private SurfaceView preview = null;
private SurfaceHolder previewHolder = null;
private Camera camera = null;
private boolean inPreview = false;
Bitmap bmp;
static Bitmap mutableBitmap;
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
File imageFileName = null;
File imageFileFolder = null;
private MediaScannerConnection msConn;
Display d;
int screenhgt, screenwdh;
ProgressDialog dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preview);
preview = (SurfaceView) findViewById(R.id.surface);
previewHolder = preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
previewHolder.setFixedSize(getWindow().getWindowManager()
.getDefaultDisplay().getWidth(), getWindow().getWindowManager()
.getDefaultDisplay().getHeight());
}
#Override
public void onResume() {
super.onResume();
camera = Camera.open();
setCameraDisplayOrientation(this, 0, camera);
}
#Override
public void onPause() {
if (inPreview) {
camera.stopPreview();
}
camera.release();
camera = null;
inPreview = false;
super.onPause();
}
private Camera.Size getBestPreviewSize(int width, int height,
Camera.Parameters parameters) {
Camera.Size result = null;
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
if (size.width <= width && size.height <= height) {
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;
if (newArea > resultArea) {
result = size;
}
}
}
}
return (result);
}
SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(previewHolder);
} catch (Throwable t) {
Log.e("PreviewDemo-surfaceCallback",
"Exception in setPreviewDisplay()", t);
Toast.makeText(Customcamera.this, t.getMessage(),
Toast.LENGTH_LONG).show();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = getBestPreviewSize(width, height, parameters);
if (size != null) {
parameters.setPreviewSize(size.width, size.height);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
setCameraDisplayOrientation(Customcamera.this, 0, camera);
camera.setParameters(parameters);
camera.startPreview();
inPreview = true;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// no-op
}
};
Camera.PictureCallback photoCallback = new Camera.PictureCallback() {
public void onPictureTaken(final byte[] data, final Camera camera) {
Log.i("onPictureTaken", "onPictureTaken");
dialog = ProgressDialog.show(Customcamera.this, "", "Saving Photo");
/*
* new Thread() { public void run() { try { // Thread.sleep(1000); }
* catch (Exception ex) { } onPictureTake(data, camera); }
* }.start();
*/
onPictureTake(data, camera);
}
};
public void onPictureTake(byte[] data, Camera camera) {
if (mutableBitmap != null) {
mutableBitmap.recycle();
}
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
savePhoto(mutableBitmap);
showPhoto();
dialog.dismiss();
}
private void showPhoto() {
Intent intent = new Intent(this, EditAndPostActivity.class);
startActivity(intent);
}
class SavePhotoTask extends AsyncTask<byte[], String, String> {
#Override
protected String doInBackground(byte[]... jpeg) {
File photo = new File(Environment.getExternalStorageDirectory(),
"photo.jpg");
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos = new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
} catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
return (null);
}
}
public void savePhoto(Bitmap bmp) {
imageFileFolder = new File(Environment.getExternalStorageDirectory(),
"Unipyx");
imageFileFolder.mkdir();
FileOutputStream out = null;
Calendar c = Calendar.getInstance();
String date = fromInt(c.get(Calendar.MONTH))
+ fromInt(c.get(Calendar.DAY_OF_MONTH))
+ fromInt(c.get(Calendar.YEAR))
+ fromInt(c.get(Calendar.HOUR_OF_DAY))
+ fromInt(c.get(Calendar.MINUTE))
+ fromInt(c.get(Calendar.SECOND));
imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
try {
out = new FileOutputStream(imageFileName);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
scanPhoto(imageFileName.toString());
out = null;
} catch (Exception e) {
e.printStackTrace();
}
}
public String fromInt(int val) {
return String.valueOf(val);
}
public void scanPhoto(final String imageFileName) {
msConn = new MediaScannerConnection(Customcamera.this,
new MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
msConn.scanFile(imageFileName, null);
Log.i("msClient obj in Photo Utility",
"connection established");
}
public void onScanCompleted(String path, Uri uri) {
msConn.disconnect();
Log.i("msClient obj in Photo Utility", "scan completed");
}
});
msConn.connect();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
onBack();
}
return super.onKeyDown(keyCode, event);
}
public void onBack() {
Log.e("onBack :", "yes");
camera.takePicture(null, null, photoCallback);
inPreview = false;
}
#Override
public void onClick(View v) {
}
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
}
When you convert the byte[] to a Bitmap in onPictureTake(), you are throwing away the orientation information included in the byte array. You should instead directly write the bytes to a File if you want to save the orientation information.

Categories

Resources