克服 Android 相机产生低分辨率的照片结果

Overcoming Photo Results from Android Cameras That Produce Low Resolution

我想用相机拍照并上传到服务器,但是当我用相机拍照并上传时,图片的分辨率很低。

代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CaptureImageFromCamera = (ImageView)findViewById(R.id.imageView);

    CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(takePictureIntent, 1);
            }
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 1)
            try {
                onCaptureImageResult(data);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

private void onCaptureImageResult(Intent data) throws IOException {
    bitmap = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes;
    bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(),
            "DCA/Attachment/" + System.currentTimeMillis() + ".png");
    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ImageViewHolder.setImageBitmap(bitmap);
}

可以看出画质是低分辨率的

有没有办法解决这个问题?

[更新]我如何解决这个问题

我看了文章here解决了这个问题

注:从起始页开始阅读

尝试使用 TextureView.SurfaceTextureListenercamera.takePicture(shutterCallback, rawCallback, pictureCallback)

对于从相机拍摄的图像,您应该考虑 JPEG 压缩。 PNG更适合颜色较少的图标。

package com.example.admin.camsdemo;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
Button captureimage;
ContentValues cv;
Uri imageUri;
ImageView imgView;
    public static final int PICTURE_RESULT=111;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        captureimage=(Button)findViewById(R.id.opencamera);
        imgView=(ImageView)findViewById(R.id.img);
        captureimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cv = new ContentValues();
                cv.put(MediaStore.Images.Media.TITLE, "My Picture");
                cv.put(MediaStore.Images.Media.DESCRIPTION, "From Camera");
                imageUri = getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, PICTURE_RESULT);
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

            case PICTURE_RESULT:
                if (requestCode == PICTURE_RESULT)
                    if (resultCode == Activity.RESULT_OK) {
                        try {
                          Bitmap thumbnail = MediaStore.Images.Media.getBitmap(
                                    getContentResolver(), imageUri);
                            imgView.setImageBitmap(thumbnail);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
        }
    }
}