簡體   English   中英

為什么使用“曲面”視圖的相機圖像與我使用Intent(ACTION_IMAGE_CAPTURE)捕獲的圖像不同

[英]why the images of camera using Surface view is not same as the image i capture using Intent(ACTION_IMAGE_CAPTURE)

我使用以下代碼捕獲圖像。 在其中,我使用Surfaceview 捕獲的圖像保存在我在android Sdcard中創建的文件夾中,但是當我在GridView顯示這些圖像時,圖像方向會有所不同。 如果我使用相機使用Intent (ACTION_IMAGE_CAPTURE)則輸出圖像定義良好。 誰能幫我

在這里,我有一個擴展Activity的類,它是一個工具SurfaceHolder.Callback

public class CameraView extends Activity implements SurfaceHolder.Callback {

       private static final String TAG = "CameraVeiw";

        private Camera camera;
        boolean previewing = false;
        SurfaceHolder surfaceHolder;
        private File file = null;
        private static String mFileName = null;

        private long name;

        final int RESULT_SAVEIMAGE = 0;
        private int w;
        private int h;

        public   static byte[] data = null;

        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.camera);
       }

我使用SurfaceView捕獲圖像

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
    surfaceHolder = surfaceView.getHolder();
    surfaceView.getWindowVisibility();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    surfaceHolder.setFixedSize(300, 300); //hard coded

    Button button2 = (Button) findViewById(R.id.picbutton);
    button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {



    camera.takePicture(new CustomShutter(), null, 
    new CustomPictureCallback());
    }
    });

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

        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();


                previewing = true;


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
             camera.setDisplayOrientation(90);        }

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
    camera = null;
    previewing = false;  
}

class CustomPictureCallback implements Camera.PictureCallback {
    private Bitmap bitmap;
public void onPictureTaken(byte[] data, Camera camera) {

             FileOutputStream outStream = null;

                try {

        String path = "/sdcard/MonsterApp/Photos/" + data+".jpg";
                outStream = new FileOutputStream(String.format(path));
                    outStream.write(data);
                    outStream.close();
                    Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                }
                Log.d(TAG, "onPictureTaken - jpeg");

                camera.startPreview();
                Log.i("Information", "Helllllooooo");   } }




class CustomShutter implements Camera.ShutterCallback {


    public void onShutter() {

    }

    }
    @Override
    public void onDestroy() {
    super.onDestroy();
    if(camera != null) {
    camera.release();
    }
    }

}

我認為當您使用Intent(ACTION_IMAGE_CAPTURE)時,它將使用默認應用程序,該應用程序從不處理設備方向。 設備始終僅保持橫向。 但在旋轉設備時以表面視圖為例,則它可以處理方向。 因此,它也旋轉相機拍攝。 我認為最好的解決方案是將相機的拍攝活動固定為風景,並在風景中設置適當的方向。 我認為您的相機可以正常工作。 如果您不明白,那么我可以向您發布一些代碼。。但是首先嘗試。我不確定,但是我認為它應該可以工作。

你能嘗試一次嗎

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

在創建時將其寫入,然后使用表面視圖捕獲。

試試這個來設置正確的方向

mCamera.setDisplayOrientation(90);

要么

mCamera.setDisplayOrientation(0);

有四個不同的角度,一個絕對可以與您的代碼一起使用。 嘗試每個。

有一個元數據,其中包含所有從android相機捕獲的圖像。 我認為您在使用表面視圖捕獲時沒有將EXIF元數據與圖像一起保存。 EXIF元數據包含拍攝圖像時手機的方向詳細信息。 因此,android系統在顯示時會根據方向細節旋轉圖像。 此詳細信息可能對您有幫助

http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM