Android में कैमरा ओरिएंटेशन मुद्दा


100

मैं एक एप्लिकेशन बना रहा हूं जो चित्र लेने के लिए कैमरे का उपयोग करता है। यहाँ ऐसा करने के लिए मेरा स्रोत कोड है:

        File file = new File(Environment.getExternalStorageDirectory(),
            imageFileName);
    imageFilePath = file.getPath();
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    startActivityForResult(intent, ACTIVITY_NATIVE_CAMERA_AQUIRE);

पर onActivityResult()विधि, मैं का उपयोग BitmapFactory.decodeStream()पिक करने के लिए छवि।

जब मैं नेक्सस एक पर अपना एप्लिकेशन चलाता हूं, तो यह अच्छी तरह से चलता है। लेकिन जब मैं सैमसंग गैलेक्सी एस या एचटीसी इंस्पायर 4 जी पर चलता हूं, तो छवि की दिशा सही नहीं है।

  • पोर्ट्रेट मोड के साथ कैप्चर करें, वास्तविक छवि (एसडी कार्ड पर सहेजें) हमेशा 90 डिग्री घूमती है।

शॉट के बाद छवि पूर्वावलोकन एसडी कार्ड पर वास्तविक छवि

शॉट के बाद छवि पूर्वावलोकन --------- एसडी कार्ड पर वास्तविक छवि

  • लैंडस्केप मोड के साथ कैप्चर करें, सभी चीजें अच्छी हैं।

शॉट के बाद छवि पूर्वावलोकन एसडी कार्ड पर वास्तविक छवि

शॉट के बाद छवि पूर्वावलोकन --------- एसडी कार्ड पर वास्तविक छवि


1
setRotation (90) ने सैमसंग गैलेक्सी नेक्सस में मेरे लिए काम किया, जबकि इसने एक्सपीरिया एस में छवि को नहीं घुमाया
StarDust

क्या कोई मुझे इस बारे में सहायता कर सकता है? मैं एक ही समस्या है stackoverflow.com/questions/28379130/…



जवाबों:


50

यहाँ कुछ समान विषय और मुद्दे हैं। चूंकि आप अपना कैमरा नहीं लिख रहे हैं, मुझे लगता है कि यह इस पर उबलता है:

कुछ डिवाइस इसे सहेजने से पहले छवि को घुमाते हैं, जबकि अन्य केवल फोटो के एक्सिफ डेटा में ओरिएंटेशन टैग जोड़ते हैं।

मैं फ़ोटो के एक्सफ़ डेटा की जाँच करने और विशेष रूप से देखने की सलाह देता हूँ

ExifInterface exif = new ExifInterface(SourceFileName);     //Since API Level 5
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);

चूंकि तस्वीर आपके ऐप में सही ढंग से प्रदर्शित हो रही है, इसलिए मुझे यकीन नहीं है कि समस्या कहां है, लेकिन यह आपको सही रास्ते पर निश्चित रूप से सेट करना चाहिए!


33
यह कुछ उपकरणों पर काम नहीं करता है, यह सभी झुकावों के लिए 0 देता है .. मुझे पता है कि यह गैलेक्सी एस इन्फ्यूज़ और सोनी एक्सपीरिया आर्क, और एस II पर होता है। दिलचस्प बात यह है कि जब इन समान छवियों को गैलरी से चुना जाता है सामग्री प्रदाता के पास उचित अभिविन्यास मूल्य है .. कोई विचार?
टोलगा ई

3
@ अभिजीत हां मैंने इसे हल करने की कोशिश की (Android और इतने पर टिकट खोला) और मुझे लगता है कि मुझे उचित और दोषपूर्ण अभिविन्यास जानकारी वाले दोनों फोन को संभालने के लिए एक उचित समाधान मिला। अपने विस्तृत उत्तर की जाँच करें जिसे मैंने अपने प्रश्न यहाँ पोस्ट किया है; stackoverflow.com/a/8864367/137404
टोलगा ई

1
@ramz मैंने खोजने के लिए इस समाधान की कोशिश की। लेकिन यह सभी झुकावों के लिए 0 लौटा रहा है। क्या आपके पास कोई विचार है कि यह सभी अभिविन्यास के लिए 0 क्यों लौटा रहा है।
डोर

1
इस समाधान के काम न करने का कारण कभी-कभी ExifInterface कंस्ट्रक्टर में उपयोग किया जाने वाला एक गलत "पथ" है। ज्यादातर किटकैट पर। यहां देखें, कैसे सही रास्ता
पाएं

1
यह हमेशा मेरे Samsung Galaxy S4 पर 0 (ExifInterface.ORIENTATION_UNDEFINED) देता है ...
valerybodak

28

मुझे सिर्फ एक ही समस्या का सामना करना पड़ा, और इसका उपयोग अभिविन्यास को सही करने के लिए किया:

public void fixOrientation() {
    if (mBitmap.getWidth() > mBitmap.getHeight()) {
        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        mBitmap = Bitmap.createBitmap(mBitmap , 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
    }
}

यदि बिटमैप की चौड़ाई ऊंचाई से अधिक है, तो लौटी हुई छवि परिदृश्य में है, इसलिए मैं इसे 90 डिग्री घुमाता हूं।

आशा है कि यह इस मुद्दे के साथ किसी और की मदद करता है।


18
क्या होगा अगर छवि वास्तव में परिदृश्य में ली गई थी? आपका कोड अभी भी इसे घुमाएगा। यह सवाल का जवाब नहीं है।
व्लादिमीर पलंत

1
मेरा अनुप्रयोग पोर्ट्रेट को बल देता है, इसलिए यह कोई समस्या नहीं है। मैंने केवल इसे समस्या के वैकल्पिक समाधान के रूप में शामिल किया है।

5
लेकिन अगर अनुप्रयोग आपको अभी भी एक परिदृश्य चित्र (चौड़ाई> ऊँचाई) का चित्र लेने के लिए मजबूर करता है और इसे घुमाया जाएगा ... यह कम से कम जो मुझे सेट हो रहा है स्क्रीनरिएशन = "पोर्ट्रेट" सब कुछ के लिए ... कैमरा अभी भी परिदृश्य ले सकता है तस्वीरें।
Ixx

पूर्ण और सही जवाब यहां मौजूद है stackoverflow.com/questions/6069122/…
शिरीष हेरवाडे

21

दो चीजों की जरूरत है:

  1. कैमरा पूर्वावलोकन आपके रोटेशन के समान ही है। इसके द्वारा सेट करेंcamera.setDisplayOrientation(result);

  2. अपने कैमरे के पूर्वावलोकन के रूप में कैप्चर की गई तस्वीर को सहेजें। इसके माध्यम से करें Camera.Parameters

    int mRotation = getCameraDisplayOrientation();
    
    Camera.Parameters parameters = camera.getParameters();
    
    parameters.setRotation(mRotation); //set rotation to save the picture
    
    camera.setDisplayOrientation(result); //set the rotation for preview camera
    
    camera.setParameters(parameters);

उम्मीद है की वो मदद करदे।


यह बात है! Camera.parameters एक बिटमैप को रेंडर करने के बिना स्नैपशॉट को बचाने के लिए वास्तव में सुविधाजनक है
23'14 को 04:35

कृपया इसे सबसे आसान उत्तर के रूप में चिह्नित करें! यह काम करो! इस आसान समाधान के बारे में बहुत खुश
काई बरगद्ट

यदि यह स्निपेट लिया जाता है, तो हम कह सकते हैं parameters.setRotation(result), नहीं?
मैट लोगन

10
यह मानता है कि आप सीधे कैमरा वर्ग का उपयोग कर रहे हैं, जब आप ACTION_IMAGE_CAPTURE आशय का उपयोग कर रहे हैं तो आप उन्हीं विकल्पों को निर्दिष्ट नहीं कर सकते हैं।
छत

1
परिणाम और getCameraDisplayOrientation () क्या है?
वेंकट

10
            int rotate = 0;
            try {
                File imageFile = new File(sourcepath);
                ExifInterface exif = new ExifInterface(
                        imageFile.getAbsolutePath());
                int orientation = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotate = 270;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotate = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotate = 90;
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            Matrix matrix = new Matrix();
    matrix.postRotate(rotate);
    bitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

यह कुछ स्पष्टीकरण देना उपयोगी होगा कि आपका कोड क्या कर रहा है, और यह अन्य उत्तरों की तुलना में अलग तरीके से कैसे कर रहा है (प्रश्न पहले से ही काफी पुराना है, और अन्य उत्तर इसलिए शायद काफी परिपक्व हैं)।
कोडेलिंग

7

एक अन्य विकल्प इस तरह परिणाम स्क्रीन में बिटमैप को घुमाने के लिए है:

ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.refresh);
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);

img.setImageDrawable(bmd);

30
यह दृष्टिकोण काम नहीं करेगा, क्योंकि यह उन उपकरणों से छवियों को घुमाता है जो अभिविन्यास को ठीक से संभालता है।
हंस्पेड

पूर्ण और सही जवाब यहां मौजूद है stackoverflow.com/questions/6069122/…
शिरीष हेरवाडे

3

मैं भी कुछ डिवाइस के लिए इस तरह की समस्या है:

private void rotateImage(final String path) {

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(Conasants.bm1, 1000,
            700, true);
    Bitmap rotatedBitmap = null;
    try {
        ExifInterface ei = new ExifInterface(path);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        Matrix matrix = new Matrix();
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.postRotate(90);
            rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                    scaledBitmap.getWidth(), scaledBitmap.getHeight(),
                    matrix, true);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.postRotate(180);
            rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                    scaledBitmap.getWidth(), scaledBitmap.getHeight(),
                    matrix, true);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.postRotate(270);
            rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                    scaledBitmap.getWidth(), scaledBitmap.getHeight(),
                    matrix, true);
            break;
        default:
            rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                    scaledBitmap.getWidth(), scaledBitmap.getHeight(),
                    matrix, true);
            break;
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
    cropImage.setImageBitmap(rotatedBitmap);
    rotatedBitmap = null;
    Conasants.bm1 = null;
}

1

इस तरह से प्रयास करें: स्थिर उरी छवि_ुरी; स्थिर बिटमैप लिया_मेज = अशक्त;

            image_uri=fileUri; // file where image has been saved

      taken_image=BitmapFactory.decodeFile(image_uri.getPath());
      try
        {
            ExifInterface exif = new ExifInterface(image_uri.getPath()); 

            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);


            switch(orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                    RotateBitmap(taken_image, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                    RotateBitmap(taken_image, 180);

                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                    RotateBitmap(taken_image, 270);

                    break;
                case ExifInterface.ORIENTATION_NORMAL:
                    taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                    RotateBitmap(taken_image, 0);

                    break;
            }

        }
        catch (OutOfMemoryError e)
        {
            Toast.makeText(getActivity(),e+"\"memory exception occured\"",Toast.LENGTH_LONG).show();


        }



public Bitmap RotateBitmap(Bitmap source, float angle) {
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);

      round_Image = source;
      round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(),   source.getHeight(), matrix, true);


  return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

}


1

फोटो के एक्सिफ़ डेटा की अधिक जाँच नहीं। ग्लाइड के साथ आसान जाओ ।

Google ने हमें Glide नामक एक लाइब्रेरी के रूप में विकसित Android के लिए एक Image Loader Library की शुरुआत की, जिसे Google ने अनुशंसित किया। Google I / O 2014 आधिकारिक एप्लिकेशन सहित अब तक कई Google ओपन सोर्स प्रोजेक्ट्स में इसका उपयोग किया गया है।

Ex: Glide.with (संदर्भ)। लोड (यूरी) .into (इमेजव्यू);

अधिक के लिए: https://github.com/bumptech/glide


1
public void setCameraPicOrientation(){
        int rotate = 0;
        try {
            File imageFile = new File(mCurrentPhotoPath);
            ExifInterface exif = new ExifInterface(
                    imageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotate = 270;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotate = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotate = 90;
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);
        int targetW = 640;
        int targetH = 640;

        /* Get the size of the image */
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        /* Figure out which way needs to be reduced less */
        int scaleFactor = 1;
        if ((targetW > 0) || (targetH > 0)) {
            scaleFactor = Math.min(photoW/targetW, photoH/targetH);
        }

        /* Set bitmap options to scale the image decode target */
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        /* Decode the JPEG file into a Bitmap */
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        bitmap= Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            /* Associate the Bitmap to the ImageView */
      imageView.setImageBitmap(bitmap);
    }

आशा है कि यह मदद करेगा !! धन्यवाद


0
    public static  int mOrientation =  1;

    OrientationEventListener myOrientationEventListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.takephoto);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


        myOrientationEventListener
        = new OrientationEventListener(getApplicationContext()) {

            @Override
            public void onOrientationChanged(int o) {
                // TODO Auto-generated method stub
                if(!isTablet(getApplicationContext()))
                {
                    if(o<=285 && o>=80)
                        mOrientation = 2;
                    else
                        mOrientation = 1;
                }
                else
                {
                    if(o<=285 && o>=80)
                        mOrientation = 1;
                    else
                        mOrientation = 2;
                }

            }
        };

        myOrientationEventListener.enable();

    }



    public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

}

मुझे आशा है कि यह मदद करेगा। धन्यवाद!


0

बस यहाँ एक ही मुद्दा है, नीचे कोड स्निपेट मेरे लिए काम करता है:

private static final String[] CONTENT_ORIENTATION = new String[] {
        MediaStore.Images.ImageColumns.ORIENTATION
};

static int getExifOrientation(ContentResolver contentResolver, Uri uri) {
    Cursor cursor = null;

    try {
        cursor = contentResolver.query(uri, CONTENT_ORIENTATION, null, null, null);
        if (cursor == null || !cursor.moveToFirst()) {
            return 0;
        }
        return cursor.getInt(0);
    } catch (RuntimeException ignored) {
        // If the orientation column doesn't exist, assume no rotation.
        return 0;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

उम्मीद है कि यह मदद करता है :)


0

इसे सरफेस कॉलबैक में आज़माएं:

Camera.Parameters parameters=mCamera.getParameters();
if(this.getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
    parameters.setRotation(90);
}else{
    parameters.setRotation(0);
}
mCamera.setParameters(parameters);

0

// बटन पर क्लिक करें

btnCamera.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                try {
                    ContentValues values;
                    values = new ContentValues();
                    values.put(MediaStore.Images.Media.TITLE, "New Picture");
                    values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
                    imageUri = context.getContentResolver().insert(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    startActivityForResult(intent, CAMERA_REQUEST);
                }catch (Exception e){}

            });

// onActivityResult विधि

   if (requestCode==CAMERA_REQUEST){
        try {
            if (imageUri!=null) {
                path = String.valueOf(new File(FilePath.getPath(context, imageUri)));
                   }  
        }catch (Exception e){
            toast("please try again "+e.getMessage());
            Log.e("image error",e.getMessage());
        }
    }

// एक क्लास फ़ाइलपैथ बनाएं

सार्वजनिक वर्ग FilePath {

public static String getPath(final Context context, final Uri uri) {

    // check here to KITKAT or new version
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {

        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/"
                        + split[1];
            }
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection,
                    selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context
 *            The context.
 * @param uri
 *            The Uri to query.
 * @param selection
 *            (Optional) Filter used in the query.
 * @param selectionArgs
 *            (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 */
public static String getDataColumn(Context context, Uri uri,
                                   String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection,
                selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

/**
 * @param uri
 *            The Uri to check.
 * @return Whether the Uri authority is ExternalStorageProvider.
 */
public static boolean isExternalStorageDocument(Uri uri) {
    return "com.android.externalstorage.documents".equals(uri
            .getAuthority());
}

/**
 * @param uri
 *            The Uri to check.
 * @return Whether the Uri authority is DownloadsProvider.
 */
public static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri
            .getAuthority());
}

/**
 * @param uri
 *            The Uri to check.
 * @return Whether the Uri authority is MediaProvider.
 */
public static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri
            .getAuthority());
}

/**
 * @param uri
 *            The Uri to check.
 * @return Whether the Uri authority is Google Photos.
 */
public static boolean isGooglePhotosUri(Uri uri) {
    return "com.google.android.apps.photos.content".equals(uri
            .getAuthority());
}

}


-1

कोड कार्यात्मक रूप से लैंडस्केप और पोट्रेट @frontCameraID = चर के लिए है, यह शो कैमरा के लिए विधि क्लासिक चाहता था

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    if(holder.getSurface() == null) {
        return;
    }
    try{
        camera.stopPreview();
    } catch (Exception e){
    }

    try{

        int orientation = getDisplayOrientation(frontCameraID);

        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
        if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        }

        parameters.setRotation(rotationPicture);
        camera.setParameters(parameters);
        camera.setDisplayOrientation(orientation);
        camera.startPreview();

    } catch (Exception e) {
        Log.i("ERROR", "Camera error changed: " + e.getMessage());
    }
}

चित्र को सहेजने के लिए अभिविन्यास y रोटेशन पाने की विधि और कैमरा @rotationPicture के पूर्वावलोकन दृश्य पर अभिविन्यास @result = ओरिएंटेशन = चित्र को सहेजने के लिए रोटेशन आवश्यक है

private int getDisplayOrientation(int cameraId) {

    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = ((Activity) context).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;
        rotationPicture = (360 - result) % 360;
    } else {
        result = (info.orientation - degrees + 360) % 360;
        rotationPicture = result;
    }

    return result;
}

कोई कोड के बारे में सवाल करता है, कृपया मुझे बताएं।


-2

पिकासो और ग्लाइड लाइब्रेरी का उपयोग करते हुए दो एक लाइन समाधान

छवि रोटेशन समस्या के लिए बहुत सारे समाधानों के साथ बहुत समय बिताने के बाद आखिरकार मुझे दो सरल समाधान मिले। हमें कोई अतिरिक्त कार्य करने की आवश्यकता नहीं है। पिकासो और ग्लाइड आपके ऐप में छवियों को संभालने के लिए एक बहुत शक्तिशाली पुस्तकालय हैं। यह इमेज EXIF ​​डेटा और ऑटो-रोटेट्स इमेजेस को रीड करेगा।

ग्लाइड लाइब्रेरी का उपयोग करके https://github.com/bumptech/glide

Glide.with(this).load("http url or sdcard url").into(imgageView);

पिकासो पुस्तकालय का उपयोग करके https://github.com/square/picasso

Picasso.with(context).load("http url or sdcard url").into(imageView);

एसओ में आपका स्वागत है। कृपया upvotes मांगने से बचें: meta.stackexchange.com/questions/194061/…
JanS
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.