1

用户可以camera在任何模式下捕获图像,但我想将结果限制为仅纵向(结果应仅纵向)。

为了实现这一点,我正在使用ExifInterface并且在某些方面我也成功了,它在LG 和 HTC 设备中运行良好,但在三星设备中无法运行

代码:

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

你的建议是可观的。

4

1 回答 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'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(); 
            }  

        }
于 2015-12-29T05:42:23.170 回答