简体   繁体   English

从相机拍摄的图像应为人像模式

[英]Captured image from camera should be in portrait mode

User can capture image from camera in any mode but I want to restrict result to be in portrait only (result should be in portrait only). 用户可以在任何模式下从camera捕获图像,但我想将结果限制为仅纵向(结果应仅纵向)。

To achieve this I am using ExifInterface and at some extends I am succeeded too, its working fine in LG & HTC devices but not working in Samsung devices . 为了实现这一点,我使用了ExifInterface并且在某种程度上,我也成功了,它在LG&HTC设备中可以 正常工作,在Samsung设备中不能工作

Code: 码:

                 ExifInterface exif = new ExifInterface(path);
                 orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                 Log.e("orientation",""+orientation);
                 Matrix m=new Matrix();

                 if((orientation==3)){

                 m.postRotate(180);
                 m.postScale((float)bm.getWidth(), (float)bm.getHeight());

//               if(m.preRotate(90)){
                 Log.e("in orientation",""+orientation);

                 bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
                 return  bitmap;
                 }
                 else if(orientation==6){

                  m.postRotate(90);

                  Log.e("in orientation",""+orientation);

                  bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
                     return  bitmap;
                 }

                 else if(orientation==8){

                  m.postRotate(270);

                  Log.e("in orientation",""+orientation);

                  bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
                     return  bitmap;
                 }
                 return bitmap;
             }
             catch (Exception e) {
             }
             return null;
         }

Your suggestions are appreciable. 您的建议是可理解的。

Try this it works for me 试试这个对我有用

public static Bitmap scaleImage(Context context, Uri photoUri) throws IOException {
            InputStream is = context.getContentResolver().openInputStream(photoUri);
            BitmapFactory.Options dbo = new BitmapFactory.Options();
            dbo.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, dbo);
            is.close();

            int rotatedWidth, rotatedHeight;
            int orientation = getOrientation(context, photoUri);

            if (orientation == 90 || orientation == 270) {
                rotatedWidth = dbo.outHeight;
                rotatedHeight = dbo.outWidth;
            } else {
                rotatedWidth = dbo.outWidth;
                rotatedHeight = dbo.outHeight;
            }

            Bitmap srcBitmap;
            is = context.getContentResolver().openInputStream(photoUri);
            if (rotatedWidth > 100 || rotatedHeight > 100) {
                float widthRatio = ((float) rotatedWidth) / ((float) 100);
                float heightRatio = ((float) rotatedHeight) / ((float) 100);
                float maxRatio = Math.max(widthRatio, heightRatio);

                // Create the bitmap from file
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = (int) maxRatio;
                srcBitmap = BitmapFactory.decodeStream(is, null, options);
            } else {
                srcBitmap = BitmapFactory.decodeStream(is);
            }
            is.close();

            /*
             * if the orientation is not 0 (or -1, which means we don't know), we
             * have to do a rotation.
             */
            if (orientation > 0) {
                Matrix matrix = new Matrix();
                matrix.postRotate(orientation);

                srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
                        srcBitmap.getHeight(), matrix, true);
            }

            String type = context.getContentResolver().getType(photoUri);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            if (type.equals("image/png")) {
                srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            } else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
                srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            }
            byte[] bMapArray = baos.toByteArray();
            baos.close();
            return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
        }

        public static int getOrientation(Context context, Uri photoUri) {
            /* it's on the external media. */
            Cursor cursor = context.getContentResolver().query(photoUri,
                    new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

            if (cursor.getCount() != 1) {
                return -1;
            }

            cursor.moveToFirst();
            return cursor.getInt(0);
        }
        private void openGalleryImage(Intent data) 
        {
            Uri selectedimg = data.getData();
            Uri uriselectedimage=data.getData();
            mString=uriselectedimage.getPath();
            try 
            {
                mInputStream= getContentResolver().openInputStream(selectedimg);
            }
            catch (FileNotFoundException e) 
            {
                e.printStackTrace();
            }

            String[] path = { MediaStore.Images.Media.DATA };
            Cursor c =  getContentResolver().query(selectedimg, path, null, null,null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(path[0]);
            selectedImagePath=c.getString(columnIndex);
            uri_outputFileUri= Uri.parse(selectedImagePath);
            c.close();
        }

        private void saveImage(String mPath) 
        {

            System.out.println("Paths  "+mPath);
                mediaDir= new File(Environment.getExternalStorageDirectory() +"/MyFolder");
                mediaDir.mkdir();


            File myImage = new File(mediaDir, Long.toString(System.currentTimeMillis()) + ".png");

            try 
            { 
                FileOutputStream out = new FileOutputStream(myImage);
                mbitmap_outputImage=BitmapFactory.decodeFile(mPath);    
                mbitmap_outputImage=Bitmap.createScaledBitmap(mbitmap_outputImage, 390, 310, true);
                mbitmap_outputImage.compress(Bitmap.CompressFormat.PNG, 100, out); 
                img.setImageBitmap(mbitmap_outputImage);
                out.flush();    
                out.close();

            } 
            catch (Exception e)
            { 
                e.printStackTrace(); 
            }  

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM