0

Are there any way to constantly set the resolution of the photo (like 256x256) that were taken by my own camera application. Here is bits of code, Thanks.

SurfaceView cameraView;
SurfaceHolder surfaceHolder;
Camera camera;

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

    File mFile=new File("/sdcard/Galmix");
    mFile.mkdir();

    cameraView = (SurfaceView)this.findViewById(R.id.CameraView);
    surfaceHolder = cameraView.getHolder();
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    surfaceHolder.addCallback(this);

    cameraView.setFocusable(true);
    cameraView.setFocusableInTouchMode(true);
    cameraView.setClickable(true);

    cameraView.setOnClickListener(this);
}
public void onClick(View v){
    camera.takePicture(null, null, this);
}

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    try{
        OutputStream imageFileOS = new FileOutputStream(String.format("/sdcard/Galmix/%d.jpg",System.currentTimeMillis()));
        imageFileOS.write(data);
        imageFileOS.flush();
        imageFileOS.close();
    } catch(FileNotFoundException e){
        Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
        t.show();
    } catch (IOException e) {
        Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
        t.show();
    }
    camera.startPreview();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
    camera.startPreview();
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
    try{
        camera.setPreviewDisplay(holder);
        Camera.Parameters parameters = camera.getParameters();

        if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
            parameters.set("orientation", "portrait");
            camera.setDisplayOrientation(90);   
        }
        List<String> colorEffects = parameters.getSupportedColorEffects();
        Iterator<String> cei = colorEffects.iterator();
        while(cei.hasNext()){
            String currentEffect = cei.next();
            if(currentEffect.equals(Camera.Parameters.EFFECT_SOLARIZE)){
                parameters.setColorEffect(Camera.Parameters.EFFECT_SOLARIZE); break;
            }
        }
        camera.setParameters(parameters);
    } catch(IOException e){
        camera.release();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
}
Lumluk Wp
  • 45
  • 3
  • 9

2 Answers2

1

See Camera Parameters for information.

Rajath
  • 11,787
  • 7
  • 48
  • 62
  • I have tried something like parameters.setPictureSize(256, 256);. But it force close. – Lumluk Wp Apr 27 '11 at 09:12
  • You need to use [getSupportedPictureSizes()](http://developer.android.com/reference/android/hardware/Camera.Parameters.html#getSupportedPictureSizes%28%29) to get a list of supported sizes, and use the one closest to what you want. – Rajath Apr 27 '11 at 09:13
  • I have upload my code so can you show me where to put those line in? I can't seem to figure it out. – Lumluk Wp Apr 28 '11 at 02:55
  • see http://stackoverflow.com/questions/4557824/android-2-2-sdk-setparameters-failed-for-camera-api-on-nexus-one – Rajath Apr 28 '11 at 05:47
1

to resolve your problem, you have to check your camera supported sizes by using this code:

Camera camera = Camera.open();
Parameters params = camera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();

Once you will se the supported Sizes, you can define them with the method :

setPictureSize(int width, int height);
Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265