当前位置: 动力学知识库 > 问答 > 编程问答 >

how to change save image Orientation in Portitate mode in android using Surfaceholder

问题描述:

public class MainActivity extends Activity {

ImageView image;

Activity context;

Preview preview;

static Camera camera;

Button exitButton;

ImageView fotoButton;

LinearLayout progressLayout;

String path = "/sdcard/KutCamera/cache/images/";

public static int count = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

context = this;

fotoButton = (ImageView) findViewById(R.id.imageView_foto);

exitButton = (Button) findViewById(R.id.button_exit);

image = (ImageView) findViewById(R.id.imageView_photo);

progressLayout = (LinearLayout) findViewById(R.id.progress_layout);

preview = new Preview(this,

(SurfaceView) findViewById(R.id.KutCameraFragment));

FrameLayout frame = (FrameLayout) findViewById(R.id.preview);

frame.addView(preview);

preview.setKeepScreenOn(true);

fotoButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

try {

takeFocusedPicture();

} catch (Exception e) {

}

exitButton.setClickable(false);

fotoButton.setClickable(false);

progressLayout.setVisibility(View.VISIBLE);

}

});

}

@Override

protected void onResume() {

super.onResume();

// TODO Auto-generated method stub

if (camera == null) {

camera = Camera.open();

camera.startPreview();

camera.setErrorCallback(new ErrorCallback() {

public void onError(int error, Camera mcamera) {

camera.release();

camera = Camera.open();

Log.d("Camera died", "error camera");

}

});

}

if (camera != null) {

if (Build.VERSION.SDK_INT >= 14)

setCameraDisplayOrientation(context,

CameraInfo.CAMERA_FACING_BACK, camera);

preview.setCamera(camera);

}

}

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);

}

Camera.AutoFocusCallback mAutoFocusCallback = new Camera.AutoFocusCallback() {

@Override

public void onAutoFocus(boolean success, Camera camera) {

try {

camera.takePicture(mShutterCallback, null, jpegCallback);

} catch (Exception e) {

}

}

};

Camera.ShutterCallback mShutterCallback = new ShutterCallback() {

@Override

public void onShutter() {

// TODO Auto-generated method stub

}

};

public void takeFocusedPicture() {

camera.autoFocus(mAutoFocusCallback);

}

PictureCallback jpegCallback = new PictureCallback() {

public void onPictureTaken(byte[] data, Camera camera) {

FileOutputStream outStream = null;

Calendar c = Calendar.getInstance();

File videoDirectory = new File(path);

if (!videoDirectory.exists()) {

videoDirectory.mkdirs();

}

try {

// Write to SD Card

outStream = new FileOutputStream(path

+ c.getTime().getSeconds() + ".jpg");

outStream.write(data);

Toast.makeText(MainActivity.this, "Image:" + count, 1000)

.show();

count++;

outStream.close();

if (count == 3) {

finish();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

}

Bitmap realImage;

final BitmapFactory.Options options = new BitmapFactory.Options();

options.inSampleSize = 5;

options.inPurgeable = true;

options.inInputShareable = true;

realImage = BitmapFactory.decodeByteArray(data, 0, data.length,

options);

if (data != null) {

int screenWidth = getResources().getDisplayMetrics().widthPixels;

int screenHeight = getResources().getDisplayMetrics().heightPixels;

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

// Notice that width and height are reversed

Bitmap scaled = Bitmap.createScaledBitmap(realImage,

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

realImage = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx,

true);

} else {

Bitmap scaled = Bitmap.createScaledBitmap(realImage,

screenWidth, screenHeight, true);

realImage = scaled;

}

}

image.setImageBitmap(realImage);

fotoButton.setClickable(true);

camera.startPreview();

progressLayout.setVisibility(View.GONE);

exitButton.setClickable(true);

}

};

}

This is my code using this i am taking Picture from camera and save in Sdcard in given path i am taking image from camera in portrait mode its working fine and save in Sd card But saved Image when i open its Landscape Mode but i want keep it in portrait mode but i am trying change many thing in surface holder class and even Main actvity class not chaining while if set camera preview in land scape then its saved Image saving in portrait mode while i want camera preview and saved image in Sd card both should in portrait mode please suggest me how to do this

网友答案:

same issue i was facing but using this code its work for me try this code:

Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                Matrix matrix = new Matrix();
                matrix.postRotate(90);    
                Bitmap bitmapFinal=null;

                bitmapFinal = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                bmp.recycle();
                bmp=null;

                FileOutputStream fos = new FileOutputStream(pictureFile);
                if(imageFormat.equals("jpg")||imageFormat.equals("jpeg"))
                {
                    bitmapFinal.compress(Bitmap.CompressFormat.JPEG, 100,fos);
                }
                else if(imageFormat.equals("png"))
                {
                    bitmapFinal.compress(Bitmap.CompressFormat.PNG, 100,fos);
                }
                else 
                {
                    bitmapFinal.compress(Bitmap.CompressFormat.JPEG, 100,fos);
                }

                bitmapFinal.recycle();
                bitmapFinal=null;

                fos.flush();
                fos.close();
                fos=null;
网友答案:
Uri imageUri = intent.getData();
            String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
            Cursor cur = managedQuery(imageUri, orientationColumn, null, null, null);
            int orientation = -1;
            if (cur != null && cur.moveToFirst()) {
                orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
            }  
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);//matrix.postRotate(90);
网友答案:

please try below rotate function just pass your bitmap and angle

public static Bitmap rotate(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, false);
    }
网友答案:

save image in local syatem

public static void saveImageTolacal(Bitmap bitmapImage,Context context){
//          String path = "/sdcard";
            String path = Environment.getExternalStorageDirectory().getPath() +  "/folder/";
            FileOutputStream fos = null;
            Calendar c = Calendar.getInstance();
            File videoDirectory = new File(path);
            if (!videoDirectory.exists()) {
                videoDirectory.mkdirs();
            }
        try {
                // Write to SD Card
             File mypath=new File(videoDirectory , c.getTime().getSeconds() + ".jpg");
            fos = new FileOutputStream(mypath);
            System.out.println("------absolute path-----"+mypath.getAbsolutePath());
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
            notifyMediaScannerService(context,mypath.getAbsolutePath());
            fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
        }
网友答案:

Please try the below code to rotate change image orientation

URI imageUri = data.getData();
ExifInterface ei = new ExifInterface(getRealPathFromURI(imageUri));
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

// float angle = 0;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
    // angle= 90;
    temp = rotate(temp, 90f);
    break;
case ExifInterface.ORIENTATION_ROTATE_180:
    // angle= 180;
    temp = rotate(temp, 180f);
    break;
case ExifInterface.ORIENTATION_ROTATE_270:
    // angle= 270;
    temp = rotate(temp, 270f);
    break;
}
网友答案:
   ExifInterface exif;    
        try {
            exif = new ExifInterface(getRealPathFromURI(imageUri));    
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION, 0);
            Log.d("EXIF", "Exif: " + orientation);
            Matrix matrix = new Matrix();
            if (orientation == 6) {
                matrix.preRotate(90);
                Log.d("EXIF", "Exif: " + orientation);
            } else if (orientation == 3) {
                matrix.preRotate(180);
                Log.d("EXIF", "Exif: " + orientation);
            } else if (orientation == 9) {
                matrix.preRotate(270);
                Log.d("EXIF", "Exif: " + orientation);
            }
            scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix,
                    true);
        } catch (IOException e) {
            e.printStackTrace();
        }
分享给朋友:
您可能感兴趣的文章:
随机阅读: