Android Camera in Portrait on SurfaceView

前端 未结 6 2061
孤城傲影
孤城傲影 2020-12-01 09:06

I tried several things to try to get the camera preview to show up in portrait on a SurfaceView. Nothing worked. I am testing on a Droid that has 2.0.1. I tr

相关标签:
6条回答
  • 2020-12-01 09:31

    There's no way to do this on many current devices, including the G1 and Droid. Take a look at the relevant bug report here:

    • http://code.google.com/p/android/issues/detail?id=1193

    Also see a comment from one of the Android engineers (Dave) here:

    • http://groups.google.com/group/android-developers/browse_thread/thread/24dfa452ffc0e049
    0 讨论(0)
  • 2020-12-01 09:36

    You can try this (good for 2.2 or below). Here I rotate the image before saving it to sd card. But it is only for portrait mode. If you had to make it for both mode then you should check camera orientation and put some check before capturing image.

    PictureCallback jpegCallback = new PictureCallback() {
       public void onPictureTaken(byte[] data, Camera camera) {
          FileOutputStream outStream = null;
          try {
            imageFilePath = getFilename();
            InputStream is = new ByteArrayInputStream(data);
            Bitmap bmp = BitmapFactory.decodeStream(is);
            // Getting width & height of the given image.
            if (bmp != null){
               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);
               ByteArrayOutputStream stream = new ByteArrayOutputStream();
               rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100, stream);
               byte[] byteArray = stream.toByteArray(); 
               outStream = new FileOutputStream
                                  (String.format(imageFilePath,System.currentTimeMillis()));
               outStream.write(byteArray);
               outStream.close();
            } else {
               outStream = new FileOutputStream
                                  (String.format(imageFilePath,System.currentTimeMillis()));
               outStream.write(data);
               outStream.close();           
            }       
    
            preview.camera.startPreview();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
      }
    };
    
    0 讨论(0)
  • 2020-12-01 09:36

    There is no need you have to set any parameters for the orientation until you need to do that explicitly. By Default, it supports this facility. In my case, i have a Activity and above that activity i have a camera view, so i didn't set any orientation for the camera properties, instead for the activity i set the orientation as portrait in the Manifest file. now the app looks and works good. Might be helpful for some one..

    Thanks.

    0 讨论(0)
  • 2020-12-01 09:39

    As of API lvl 8, this is available:

    public final void setDisplayOrientation (int degrees)

    i.e. with portait in the manifest:

    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = Camera.open();
        mCamera.setDisplayOrientation(90);
    
    0 讨论(0)
  • 2020-12-01 09:46

    i have a working solution for portrait mode working in 2.1 (tested on Desire) maybe less.

    Activity screen orientation is set to portrait. (android:screenOrientation="portrait")

    the camera parameters:

    Camera.Parameters p = mCamera.getParameters();

     p.set("jpeg-quality", 100);
     p.set("orientation", "landscape");
     p.set("rotation", 90);
     p.setPictureFormat(PixelFormat.JPEG);
     p.setPreviewSize(h, w);// here w h are reversed
     mCamera.setParameters(p);
    

    and the image will be portrait.

    SurfaceHolder you use for camera must be at a size compatible with phone preview size usualy screen resolution.

    Funny on Desire 2.2 is not working... Here is the fix:

       At surfaceCreated(..) or when you have this line
    camera = Camera.open();
           add
    camera.setDisplayOrientation(90);//only 2.2>
    
    Camera.Parameters p = camera.getParameters();
        p.set("jpeg-quality", 100);
    p.setRotation(90);
    p.setPictureFormat(PixelFormat.JPEG);
    p.setPreviewSize(h, w);
    camera.setParameters(p);
    
    0 讨论(0)
  • 2020-12-01 09:50

    The link Roman gave of the issue thread has a workable solution that I'm using now.

    Find it here: http://code.google.com/p/android/issues/detail?id=1193#c26

    0 讨论(0)
提交回复
热议问题