【问题标题】:Issues taking picture with Android (Vertical Camera | Portrait)使用 Android 拍照的问题(垂直相机 | 人像)
【发布时间】:2012-01-07 20:30:35
【问题描述】:

下面的代码显示了相机的垂直预览,它的工作原理.. 但!!我拍了一张风景照! :(

如何垂直构建它? 我有垂直的预览视图,但我无法垂直保存图片。

问候和感谢, 弗兰

点击

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


                }
};

【问题讨论】:

    标签: android photo portrait take


    【解决方案1】:

    这是我实施的解决方案。它完美地工作。我希望这会有所帮助。

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

    【讨论】:

    • 我不知道你从哪里得到 bMap 变量,并且似乎没有声明方向变量。
    【解决方案2】:
    Camera.Parameters param;
    param = camera.getParameters();
    
    Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    if(display.getRotation() == Surface.ROTATION_0)
    {
        camera.setDisplayOrientation(90);
        param.setRotation(90);
    }
    if(display.getRotation() == Surface.ROTATION_270)
    {
        camera.setDisplayOrientation(180);
        param.setRotation(180);
    }
    
    camera.setParameters(param);
    camera.takePicture(shutterCallback, rawCallback, jpegCallback);
    

    【讨论】:

      【解决方案3】:

      要设置图像的方向,请使用以下代码:

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

      【讨论】:

        【解决方案4】:

        您实际上不需要旋转位图。这是消耗内存/速度慢等...看起来在您的情况下(拍摄后立即保存到文件)更新Exif tags in a JPEG file 更好,例如:

            int degrees = 90;
            ExifInterface exif = new ExifInterface(path);
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(degrees));
            exif.saveAttributes();
        

        【讨论】:

        • 对我不起作用。我通过了这条路径,但图像仍处于横向模式,/storage/sdcard0/Pictures/Hijab2Go/IMG_20140318_114648.jpg
        猜你喜欢
        • 2011-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-23
        • 1970-01-01
        相关资源
        最近更新 更多