Android相机图片大小

时间:2011-07-20 04:05:26

标签: android image camera image-size

在我的应用程序中,我能够使用相机类运行代码来拍照,但它给了我2048 x 1536像素的图像尺寸。

当我使用我的Android设备的默认相机时,它给我2048 x 1232像素的图像大小。

现在,问题是,当我拍照时,如何让我的应用程序给我相同的图像尺寸,如默认相机(2048 x 1232)?

我有这些代码:

CameraActivity.java

public class CameraActivity extends Activity {
    private static final String TAG = "CameraDemo";
    Preview preview; // <1>
    FrameLayout buttonClick; // <2>

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);

        Toast.makeText(getBaseContext(), "Touch the screen to take picture.", Toast.LENGTH_SHORT).show();

        preview = new Preview(this); // <3>
        ((FrameLayout) findViewById(R.id.preview)).addView(preview); // <4>

        //buttonClick = (Button) findViewById(R.id.buttonClick);

        buttonClick = (FrameLayout) findViewById(R.id.preview);

        buttonClick.setOnClickListener(new OnClickListener() {
            public void onClick(View v) { // <5>
                preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
            }
        });

        Log.d(TAG, "onCreate'd");
    }

    // Called when shutter is opened
    ShutterCallback shutterCallback = new ShutterCallback() { // <6>
        public void onShutter() {
            Log.d(TAG, "onShutter'd");
        }
    };

    //Handles data for raw picture
    PictureCallback rawCallback = new PictureCallback() { // <7>
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.d(TAG, "onPictureTaken - raw");
        }
    };


    // Handles data for jpeg picture
    PictureCallback jpegCallback = new PictureCallback() { // <8>
        public void onPictureTaken(byte[] data, Camera camera) {


            FileOutputStream outStream = null;
            try {

                //Write to SD Card
                outStream = new FileOutputStream(
                    String.format(
                            Environment.getExternalStorageDirectory() + "/Engagia/AudienceImages/" + CameraActivity.this.sessionNumber + ".jpg",
                            System.currentTimeMillis()
                    )); // <9>

                outStream.write(data);
                outStream.close();

                Toast.makeText(getBaseContext(), "Preview", Toast.LENGTH_SHORT).show();


                Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
            } catch (FileNotFoundException e) { // <10>
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {

            }
            Log.d(TAG, "onPictureTaken - jpeg");
        }
    };
}

Preview.java

package com.first.Engagia;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class Preview extends SurfaceView implements SurfaceHolder.Callback { // <1>
    private static final String TAG = "Preview";

    SurfaceHolder mHolder;  // <2>
    public Camera camera; // <3>

    Preview(Context context) {
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();  // <4>
        mHolder.addCallback(this);  // <5>
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // <6>
    }


    //Called once the holder is ready
    public void surfaceCreated(SurfaceHolder holder) {  // <7>
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        camera = Camera.open(); // <8>
        try {
            camera.setPreviewDisplay(holder);  // <9>

            camera.setPreviewCallback(new PreviewCallback() { // <10>

                // Called for each frame previewed
                public void onPreviewFrame(byte[] data, Camera camera) {  // <11>
                    Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
                    Preview.this.invalidate();  // <12>
                }

            });
        } catch (IOException e) { // <13>
            e.printStackTrace();
        }
    }

  // Called when the holder is destroyed
  public void surfaceDestroyed(SurfaceHolder holder) {  // <14>
    camera.stopPreview();
    camera = null;
  }

  // Called when holder has changed
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // <15>
    camera.startPreview();
  }

}

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preview" 
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/buttonClick"
android:text="Click"
android:layout_gravity="right|bottom" />

</FrameLayout>

3 个答案:

答案 0 :(得分:15)

您可以在相机参数对象上使用setPictureSize()来配置捕获的大小:

http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPictureSize(int,int)

一般来说,首先你应该调用getSupportedPictureSizes()以确保你要求硬件支持的分辨率,但听起来你已经知道了尺寸。

答案 1 :(得分:5)

把这段代码

Parameters parameters = camera.getParameters();
parameters.set("jpeg-quality", 70);
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setPictureSize(2048, 1232);
camera.setParameters(parameters);
调用

后,在您的surfaceCreated函数中

camera = Camera.open();

你找到了解决方案......

答案 2 :(得分:0)

烯 点击here

单击here

我的工作中遇到同样的问题。 我用它来解决我的问题。

例如我在SurfaceView(CameraPreview)中使用undercode

        Camera.Parameters parameters = camera.getParameters();
        pictureSize = MyCamPara.getInstance().getPictureSize(
                parameters.getSupportedPictureSizes(), 800);
        previewSize = MyCamPara.getInstance().getPreviewSize(
                parameters.getSupportedPreviewSizes(), getHeight());
        if (previewSize != null)
            parameters
                    .setPreviewSize(previewSize.width, previewSize.height);
        if (pictureSize != null)
            parameters
                    .setPictureSize(pictureSize.width, pictureSize.height);
        camera.setPreviewDisplay(holder);
        camera.setParameters(parameters);
        camera.startPreview();