Android 将照片保存为人像

Android 将照片保存为人像,android,Android,我正在运行一个应用程序,它将始终在手机上运行。拍照时,我可以在手机上看到纵向的图像,但是,当它保存到SD卡时,图像总是横向的。这是我所做的,我用这是清单 <uses-feature android:name="android.hardware.screen.portrait" /> 我使用以下代码完成了上述操作 PictureCallback jpegCallback = new PictureCallback() { @Override pub

我正在运行一个应用程序,它将始终在手机上运行。拍照时,我可以在手机上看到纵向的图像,但是,当它保存到SD卡时,图像总是横向的。这是我所做的,我用这是清单

   <uses-feature android:name="android.hardware.screen.portrait" />

我使用以下代码完成了上述操作

   PictureCallback jpegCallback = new PictureCallback() {
    @Override
        public void onPictureTaken(byte[] data, Camera camera) { 
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        options.inDither = false; // Disable Dithering mode
        options.inPurgeable = true; // Tell to gc that whether it needs free
                                    // memory, the Bitmap can be cleared
        options.inInputShareable = true; // Which kind of reference will be
                                            // used to recover the Bitmap
                                            // data after being clear, when
                                            // it will be used in the future
        options.inTempStorage = new byte[32 * 1024];
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length, options);

        int orientation;
        // others devices
        if(bMap.getHeight() < bMap.getWidth()){
            orientation = 90;
        } else {
            orientation = 0;
        }

        Bitmap bMapRotate;
        if (orientation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);
            bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(),
                    bMap.getHeight(), matrix, true);
        } else
            bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(),
                    bMap.getHeight(), true);


        FileOutputStream out;
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
        try {
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = "/" + System.currentTimeMillis() + ".jpg";

            out = new FileOutputStream(baseDir + fileName);
            bMapRotate.compress(Bitmap.CompressFormat.JPEG, 50, out);
            if (bMapRotate != null) {
                bMapRotate.recycle();
                bMapRotate = null;
            }


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
};
PictureCallback jpegCallback=新建PictureCallback(){
@凌驾
公共无效onPictureTaken(字节[]数据,摄像头){
BitmapFactory.Options=new-BitmapFactory.Options();
options.inSampleSize=1;
options.inDither=false;//禁用抖动模式
options.inpurgable=true;//告诉gc它是否需要免费
//内存中,可以清除位图
options.inInputShareable=true;//将使用哪种类型的引用
//用于恢复位图
//数据被清除后,当
//它将在将来使用
options.inTempStorage=新字节[32*1024];
options.inPreferredConfig=Bitmap.Config.RGB_565;
位图bMap=BitmapFactory.decodeByteArray(数据,0,数据长度,选项);
智力定向;
//其他设备
if(bMap.getHeight()
    PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) { 
            FileOutputStream outStream = null;
            boolean mExternalStorageAvailable = false;
            boolean mExternalStorageWriteable = false;
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                // We can read and write the media
                mExternalStorageAvailable = mExternalStorageWriteable = true;
                Log.d(TAG, "Can Write ");
                try {
                    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
                    String fileName = "/" + System.currentTimeMillis() + ".jpg";
                    Log.d(TAG, "File: " + baseDir + fileName);
                    outStream = new FileOutputStream(baseDir + fileName); 
                    outStream.write(data);
                    outStream.close();

                    Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                }

            } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
                // We can only read the media
                mExternalStorageAvailable = true;
                mExternalStorageWriteable = false;
                Log.d(TAG, "Cant Write ");
            } else {
                // Something else is wrong. It may be one of many other states, but all we need
                //  to know is we can neither read nor write
                mExternalStorageAvailable = mExternalStorageWriteable = false;
                Log.d(TAG, "Other Error ");
            }
                }
    };
   PictureCallback jpegCallback = new PictureCallback() {
    @Override
        public void onPictureTaken(byte[] data, Camera camera) { 
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        options.inDither = false; // Disable Dithering mode
        options.inPurgeable = true; // Tell to gc that whether it needs free
                                    // memory, the Bitmap can be cleared
        options.inInputShareable = true; // Which kind of reference will be
                                            // used to recover the Bitmap
                                            // data after being clear, when
                                            // it will be used in the future
        options.inTempStorage = new byte[32 * 1024];
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length, options);

        int orientation;
        // others devices
        if(bMap.getHeight() < bMap.getWidth()){
            orientation = 90;
        } else {
            orientation = 0;
        }

        Bitmap bMapRotate;
        if (orientation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);
            bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(),
                    bMap.getHeight(), matrix, true);
        } else
            bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(),
                    bMap.getHeight(), true);


        FileOutputStream out;
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
        try {
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = "/" + System.currentTimeMillis() + ".jpg";

            out = new FileOutputStream(baseDir + fileName);
            bMapRotate.compress(Bitmap.CompressFormat.JPEG, 50, out);
            if (bMapRotate != null) {
                bMapRotate.recycle();
                bMapRotate = null;
            }


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
};