2

我正在使用 Surfaceholder 和相机拍摄图像,但是当我在 HTC Desire S 上拍摄时,拍摄的图像看起来像这样。这不是它在预览视图中的样子。

我保存后的图片

它适用于我一直在测试的其他两部手机,所以它应该是一个依赖于设备的问题,但我不知道是什么。

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_camera_overlay);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


            getWindow().setFormat(PixelFormat.UNKNOWN);
            surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
            surfaceHolder = surfaceView.getHolder();
            surfaceHolder.addCallback(this);
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            controlInflater = LayoutInflater.from(getBaseContext());
            View viewControl = controlInflater.inflate(R.layout.overlay, null);
            LayoutParams layoutParamsControl
            = new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT);
            this.addContentView(viewControl, layoutParamsControl);

            Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
            buttonTakePicture.setOnClickListener(new Button.OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    camera.takePicture(myShutterCallback,
                            myPictureCallback_RAW, myPictureCallback_JPG);
                }});


        }

...

PictureCallback myPictureCallback_JPG = new PictureCallback(){

                @Override
                public void onPictureTaken(byte[] byteArray, Camera arg1) {
                    //Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
                    OutputStream imageFileOS;

                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
                    String date = dateFormat.format(new Date());

                    String _path = Environment.getExternalStorageDirectory() + File.separator + "xxx" + File.separator + "Livboj" + date + ".jpg";
                    File file = new File( _path );
                    Uri uriTarget = Uri.fromFile( file );

                    try {
                        imageFileOS = getContentResolver().openOutputStream(uriTarget);
                        imageFileOS.write(byteArray);
                        imageFileOS.flush();
                        imageFileOS.close();
                        Toast.makeText(CameraOverlayActivity.this,"Image saved: " + uriTarget.toString(),Toast.LENGTH_LONG).show();

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();

                    } catch (IOException e) {
                        e.printStackTrace();

                    }           


                    camera.startPreview();

                }};

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

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

                Parameters parameters = camera.getParameters();
                Camera.Size size = getBestPreviewSize(width, height);
                 parameters.setPreviewSize(size.width, size.height);
                Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

                if(display.getRotation() == Surface.ROTATION_0)
                {

                    camera.setDisplayOrientation(90);
                }

                if(display.getRotation() == Surface.ROTATION_90)
                {

                }

                if(display.getRotation() == Surface.ROTATION_180)
                {

                }

                if(display.getRotation() == Surface.ROTATION_270)
                {

                    camera.setDisplayOrientation(180);
                }

                camera.setParameters(parameters);
                previewCamera();

            }

            private Camera.Size getBestPreviewSize(int width, int height)
            {
                    Camera.Size result=null;    
                    Camera.Parameters p = camera.getParameters();
                    for (Camera.Size size : p.getSupportedPreviewSizes()) {
                        if (size.width<=width && size.height<=height) {
                            if (result==null) {
                                result=size;
                            } else {
                                int resultArea=result.width*result.height;
                                int newArea=size.width*size.height;

                                if (newArea>resultArea) {
                                    result=size;
                                }
                            }
                        }
                    }
                return result;

            }




            public void previewCamera()
            {        
                try 
                {           
                    camera.setPreviewDisplay(surfaceHolder);          
                    camera.startPreview();
                    previewing = true;
                }
                catch(Exception e)
                {
                    Log.d("Camera", "Cannot start preview", e);    
                }
            }

我不知道问题出在保存或捕获中。有没有人遇到过同样的问题?提前致谢!

4

0 回答 0