Захваченное изображение с камеры должно быть в портретном режиме

1

Пользователь может снимать изображение с camera в любом режиме, но я хочу ограничить результат только портретированием (результат должен быть только на портрете).

Чтобы добиться этого, я использую ExifInterface и в некоторых случаях мне это тоже удается, он отлично работает на устройствах LG и HTC, но не работает на устройствах Samsung.

Код:

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

Ваши предложения заметны.

Теги:
android-imageview
android-orientation

1 ответ

0

Попробуйте это, это работает для меня

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

        }

Ещё вопросы

Сообщество Overcoder
Наверх
Меню