Проблемы с фотографией на Android (вертикальная камера | портрет)

С помощью следующего кода показан предварительный просмотр камеры по вертикали, и это работает.. НО!! Я получаю фото в пейзаже! :(

Как я могу построить его вертикально? У меня есть предварительный просмотр по вертикали, но я не могу сохранить изображение по вертикали.

привет и спасибо, Фрэн

НАЖМИТЕ

public void onClick(View arg0) {
       camera.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);
}

ПРЕДВАРИТЕЛЬНЫЙ ПРОСМОТР

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    if (previewing) {
        camera.stopPreview();
        previewing = false;
    }
    if (camera != null) {
        /*
         * camera.setPreviewDisplay(surfaceHolder); camera.startPreview();
         * previewing = true;
         */
        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(width, height);
        camera.setParameters(parameters);

        // Condicions per utilitzar la orientacio adecuada segons el sdk
        if (Integer.parseInt(Build.VERSION.SDK) >= 8)
            setDisplayOrientation(camera, 90);
        else {
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                parameters.set("orientation", "portrait");
                parameters.set("rotation", 90);
            }
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                parameters.set("orientation", "landscape");
                parameters.set("rotation", 90);
            }
            camera.setParameters(parameters);
        }

        // camera.startPreview();
        previewing = true;
    }
}

СДЕЛАТЬ ФОТО

PictureCallback myPictureCallback_JPG = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // intentar canvia horientacio
        Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
        //bitmapPicture.

        OutputStream imageFileOS;
        try {
            imageFileOS = new FileOutputStream(String.format("/sdcard/DCIM/iboo/captura.jpg"));
            imageFileOS.write(arg0);
            imageFileOS.flush();
            imageFileOS.close();
            // Missatge en cas de capturar l'imatge correctament
            Toast.makeText(Principal.this, "Imatge Capturada!",
                    Toast.LENGTH_LONG).show();
            MostrarFoto("/sdcard/DCIM/iboo/captura.jpg");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // camera.startPreview();


                }
};

person ephramd    schedule 28.11.2011    source источник


Ответы (3)


Это решение, которое я реализовал. Это работает отлично. Надеюсь, это поможет.

PictureCallback myPictureCallback_JPG = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 6;
        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;
        bMap = BitmapFactory.decodeByteArray(arg0, 0, arg0.length, options);

        // 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;
        try {
            out = new FileOutputStream(
                    String.format("/sdcard/DCIM/test/screen.jpg"));
            bMapRotate.compress(Bitmap.CompressFormat.JPEG, 90, out);
            if (bMapRotate != null) {
                bMapRotate.recycle();
                bMapRotate = null;
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        camera.startPreview();
        MostrarFoto(imageInSD);
        editor = prefs.edit();
        editor.putString("PathSeleccio", imageInSD);
        editor.commit();
    }
};
person ephramd    schedule 21.12.2011
comment
я не знаю, где вы берете переменную bMap, и переменная ориентации, похоже, не объявлена. - person jimbob; 07.06.2013

Для установки ориентации изображения используйте следующий код:

Matrix mat = new Matrix();
mat.postRotate(90);
image_to_upload = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(), myImage.getHeight(), mat, true);
person Netverse    schedule 07.06.2012

На самом деле вам не нужно вращать растровое изображение. Это занимает много памяти/медленно и т. д. Похоже, в вашем случае (сохранение в файл сразу после снимка) лучше обновить Теги Exif в файле JPEG, например:

    int degrees = 90;
    ExifInterface exif = new ExifInterface(path);
    exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(degrees));
    exif.saveAttributes();
person user2399321    schedule 13.02.2014
comment
мне не помогло. Я прошел этот путь, но изображение все еще находится в ландшафтном режиме, /storage/sdcard0/Pictures/Hijab2Go/IMG_20140318_114648.jpg - person Hesam; 18.03.2014