Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

android - How do you take high resolution images using Camera.takePicture()?

I'm implementing an in-app camera and every time I take a picture my image is 320x240, though my phone's camera is capable of much higher resolutions (Galaxy Nexus). I couldn't find a parameter for setting the resolution, so how do I up the resolution of images I am taking? Here's the relevant code:

@Override
public void surfaceCreated( SurfaceHolder holder ) {

    setSurfaceHolder( holder );
}

@Override
public void surfaceDestroyed( SurfaceHolder holder ) {

    setSurfaceHolder( null );
}

private void attachCameraToSurface( Camera camera, SurfaceHolder holder ) {

    try {
        camera.setPreviewDisplay( holder );
        setInPreview( true );
    }
    catch (IOException e) {
        Log.e( "CameraFragment", "Exception in attachCameraToSurface()", e );
    }
}

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

    Camera camera = getCamera();
    if ( camera == null ) {
        return;
    }

    Camera.Size size = getCameraPreviewSize( camera );
    Camera.Parameters params = camera.getParameters();
    params.setPreviewSize( size.width, size.height );
    camera.setParameters( params );

    attachCameraToSurface( camera, holder );

    startPreview();
}

private Camera.Size getCameraPreviewSize( Camera camera ) {

    Camera.Parameters params = camera.getParameters();
    List<Camera.Size> supportedSizes = params.getSupportedPreviewSizes();

    Rect frame = getSurfaceHolder().getSurfaceFrame();
    int width = frame.width();
    int height = frame.height();

    for (Camera.Size size : supportedSizes) {
        if ( size.width >= width || size.height >= height ) {
            return size;
        }
    }

    return supportedSizes.get( 0 );
}

@Override
public void onClick( View v ) {

    switch (v.getId()) {
        case R.id.camera_action_ImageView:
            getCamera().takePicture( getShutterCallback(), null, new JpegPictureCallback() );
        break;
    }
}

private class JpegPictureCallback implements PictureCallback {

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

        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeByteArray( data, 0, data.length );
        }
        catch (OutOfMemoryError e) {
            Log.e( "CameraFragment", "Out of memory decoding image from camera.", e );
            return;
        }
        data = null;

        if ( bitmap != null ) {
            new SavePictureWorker( getActivity() ).execute( bitmap );
        }

        startPreview();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use params.setPictureSize(). See Camera.Parameters for more information.

Ensure that you call params.getSupportedPictureSizes() first to get an array of supported picture sizes. Then choose a size from the list and use the setPictureSize() method to set the desired size.

Camera.Parameters params = camera.getParameters();
List<Camera.Size> supportedSizes = params.getSupportedPictureSizes();
.
.
Camera.Size sizePicture = (one of the sizes from supportedSizes list);
.
params.setPictureSize(sizePicture.width, sizePicture.height);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...