3

问题:

当我使用我的应用程序拍摄照片并将其存储在 SD 上时,分辨率为 160x120。如果使用普通相机,照片的分辨率为 1920x2560。

所以,请帮我说我必须做些什么来强制相机,从应用程序开始,以标准高分辨率拍照?

这是我用于启动相机意图和保存照片的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(cameraIntent, CAMERA_REQUEST); 
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST) { 

    Bundle xz = data.getExtras();
    if (xz!=null) {

    Bitmap image = (Bitmap) data.getExtras().get("data");

    String filePath = "/mnt/sdcard/DCIM/"; 
    filePath += "hml.png";
 try {
  image.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(filePath)));
 } 
    catch (FileNotFoundException e) {
                 // TODO Auto-generated catch block
                e.printStackTrace();}  
    catch (NullPointerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();       } 
    }}

谢谢!

4

2 回答 2

4

If you don't supply a uri for the output image, the camera intent will return only a thumbnail of the picture. Follow these instructions on how to do this properly: http://www.tutorialforandroid.com/2010/10/take-picture-in-android-with.html

于 2012-04-22T22:44:24.743 回答
1

我发现了同样的问题。也许我们使用前置摄像头。我发现还有另一个可以使用相机并发现代码崩溃。

AndroidManifest.xml:

    </application>
    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CAMERA" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/></manifest>

源代码:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private CameraView cv;
    private Camera mCamera = null;
    private Bitmap mBitmap = null; 

    public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {
            Log.i("yao","onPictureTaken");
            Toast.makeText(getApplicationContext(), "saving……", Toast.LENGTH_LONG).show();
            mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            File file = new File("/sdcard/YY"+ new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg");
            try {
                file.createNewFile();
                BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
                mBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
                os.flush();
                os.close();
                Toast.makeText(getApplicationContext(), "save completed", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        FrameLayout  fl = new FrameLayout(this);     

        cv = new CameraView(this);
        fl.addView(cv);

        TextView tv = new TextView(this);
        tv.setText("take a picture");
        fl.addView(tv);

        setContentView(fl);

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.i("yao","MainActivity.onKeyDown");
        if (keyCode == KeyEvent.KEYCODE_CAMERA) {
            if (mCamera != null) {
                Log.i("yao","mCamera.takePicture");
                mCamera.takePicture(null, null, pictureCallback);
            }
        }
        return cv.onKeyDown(keyCode, event);
    }

    class CameraView extends SurfaceView {

        private SurfaceHolder holder = null;

        public CameraView(Context context) {
            super(context);
            Log.i("yao","CameraView");

            holder = this.getHolder();
            holder.addCallback(new SurfaceHolder.Callback() {

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                    mCamera.stopPreview();
                    mCamera.release();
                    mCamera = null;
                }

                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    mCamera = Camera.open();
                    try {
                        mCamera.setPreviewDisplay(holder);
                    } catch (IOException e) {
                        mCamera.release();
                        mCamera = null;
                    }

                }

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

                    Camera.Parameters parameters = mCamera.getParameters();
                    parameters.setPictureFormat(PixelFormat.JPEG);
                    parameters.setPreviewSize(854, 480);
                    parameters.setFocusMode("auto");
                    parameters.setPictureSize(2592, 1456);
                    mCamera.setParameters(parameters);
                    mCamera.startPreview();
                }
            });
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }

    }

}
于 2012-04-05T03:29:56.280 回答