0

I implement surface in my application and it can run on some devices (galaxy, htc, ...) but in sony Xperia when my activity loaded I have an error in set parameters, how should I do to Solution this error?

Activity :

public class TakePhotoActivity extends Activity  implements SurfaceHolder.Callback, Camera.ShutterCallback, Camera.PictureCallback{



Camera mCamera;
SurfaceView mPreview;

boolean isPreviewRunning;




protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo_capture);

    mPreview = (SurfaceView)findViewById(R.id.preview);
    mPreview.getHolder().addCallback(this);
    mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

  mCamera = Camera.open();

}



@Override
public void onPause() {

        super.onPause();
        mCamera.stopPreview();
        finish();

}

@Override
public void onDestroy() {
    super.onDestroy();
    mCamera.release();

}

public void onCancelClick(View v) {
    finish();
}

public void onSnapClick(View v) {
    mCamera.takePicture(this, null, null, this);
 }

public void onSaveClick(View v) {

}

@Override
public void onShutter() {
    //Toast.makeText(this, "Click!", Toast.LENGTH_SHORT).show();
}



@Override
public void onPictureTaken(byte[] data, Camera camera) {

    try {

       /*try {
            File file = new File(Environment.getExternalStorageDirectory().getPath() + File.separator +"DocumentsWallet" );
            if( ! file.exists()){
                file.mkdirs();
            }

            FileOutputStream out = new FileOutputStream(file.getPath()+"/pic.jpeg");
            out.write(data);
            out.flush();
            out.close();
        }catch(Exception ex){
            Toast.makeText(this,ex.getMessage(),Toast.LENGTH_LONG).show();
        }*/


        Photos img=new Photos();

        Bitmap bmp=img.convertArrayByteToBitmap(data);
        Bitmap bitmap=img.rotateImage(bmp,90);
        showPhoto(bitmap);


    } catch (Exception e) {
        Toast.makeText(this,"Error in onPictureTaken method : "+e.getMessage(),Toast.LENGTH_LONG).show();

    }

    camera.startPreview();

}





private void showPhoto(Bitmap bitmap) {

    ImageView img = (ImageView) findViewById(R.id.imageView2);

    mPreview.setVisibility(View.INVISIBLE);
    img.setImageBitmap(bitmap);
    img.setVisibility(View.VISIBLE);
 }


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


    try {

        if (isPreviewRunning) {
            mCamera.stopPreview();
        }


        Camera.Parameters params = mCamera.getParameters();
        Camera.Size selected = getBestPreviewSize();
        params.setPreviewSize(selected.width, selected.height);
        params.setPictureSize(selected.width, selected.height);

        setCameraDisplayOrientation();
        params.setPictureFormat(PixelFormat.JPEG);
        mCamera.setParameters(params);
        isPreviewRunning = true;
        mCamera.startPreview();
    }catch (Exception ex){

    }

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        mCamera.setPreviewDisplay(mPreview.getHolder());

    } catch (Exception e) {
        Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

    isPreviewRunning = false;
}





private Camera.Size getBestPreviewSize( ) {



    List<Camera.Size> sizeList = mCamera.getParameters().getSupportedPreviewSizes();
    Toast.makeText(this,sizeList.size()+"",Toast.LENGTH_LONG).show();
    Camera.Size bestSize = sizeList.get(0);
    for(int i = 1; i < sizeList.size(); i++){

        if( (sizeList.get(i).width * sizeList.get(i).height) > ( bestSize.width * bestSize.height  ) ){
            bestSize = sizeList.get(i);
         }
    }

    return bestSize;
}




private void setCameraDisplayOrientation(){

    try {

        android.hardware.Camera.CameraInfo info =
                new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
        int rotation = getWindowManager().getDefaultDisplay()
                .getRotation();
        int degrees = 0;
        switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }

        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360;  // compensate the mirror
        } else {  // back-facing
            result = (info.orientation - degrees + 360) % 360;
        }
        mCamera.setDisplayOrientation(result);

    }catch (Exception ex){
        Toast.makeText(this,"Error in setCameraDisplayOrientation() method :  "+ex.getMessage(),Toast.LENGTH_LONG).show();
    }
  }

}

when i deleted and params.setPictureSize(selected.width, selected.height); error solved , but i need the best size for my image . any one can help me ?

MrNadimi
  • 1,424
  • 4
  • 18
  • 33

1 Answers1

2

you are adding the same height, width in picture size as preview size. I am not sure in case of Sony Xperia but some of the devices might not have the same ratios for preview size and picture size. So the best option is to get aspect ratio of your selected size (you get from getBestPreviewSize() ) and find the best match of picture size of same aspect ratio from List<Size> imageSizes = mCamera.getParameters().getSupportedPictureSizes();. You can check the detailed answer here

Community
  • 1
  • 1
Junaid Hafeez
  • 1,618
  • 1
  • 16
  • 25