当我从相机捕捉图像时,它会失去图像的质量?该怎么办?

时间:2016-05-13 05:37:51

标签: android camera imageview

我点击图片的代码是:

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 startActivityForResult(intent, 300);

活动代码结果:

if (requestCode == 300) { 
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".jpg");

    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();
    }

    if(number.equalsIgnoreCase("1"))
    {
        imageViewOneNumber.setImageBitmap(thumbnail);
            image1="";
            image1= getEncoded64ImageStringFromBitmap(thumbnail);
    }
    else
    {
        RelativeLayoutImage2.setVisibility(View.GONE);
        FrameImage2.setVisibility(View.VISIBLE);
        imageViewTwoNumber.setImageBitmap(thumbnail);
        image2="";
        image2= getEncoded64ImageStringFromBitmap(thumbnail);
    }
}

相机拍摄演示图片: Image which i got

请帮我解决这个问题。当我从相机中单击照片时,它会缩小图像的大小。

4 个答案:

答案 0 :(得分:3)

来自docs

  

如果您提供照片,Android相机应用程序会保存完整尺寸的照片   要保存的文件。您必须提供完全限定的文件名   相机应用程序应该保存照片。

那里的示例代码

  File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            ...
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }

String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

答案 1 :(得分:1)

像那样实现:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File image = createImage(this);
                Uri uri = Uri.parse("file://" + image.getAbsolutePath());
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                startActivityForResult(intent, CAMERA_REQUEST);

public File createImage(Context context) throws IOException {
        File dir = new File(Environment.getExternalStorageDirectory() + "/" + context.getString(R.string.company_name) + "/Images");
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                throw new IOException("Something wrong happened at" + dir);
            }
        }
        String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss", Locale.getDefault()).format(new Date());
        String imageName = context.getString(R.string.app_name) + "_" + timeStamp + ".jpg";
        return new File(dir.getPath() + File.separator + imageName);
    }

最后在onActivityResult()你可以得到你的形象:

if (requestCode == CAMERA_REQUEST) {
            //Here you can load image by Uri
        }

答案 2 :(得分:1)

开始意图

 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, 300);
onActivityResult(){..}

中的

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{  
    //Check that request code matches ours:
    if (requestCode == 300) 
    {
        //Get our saved file into a bitmap object:
       File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
       Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
    }
}

这是decodeSampledBitmapFromFile()

    public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
{ // BEST QUALITY MATCH

    //First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize, Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    int inSampleSize = 1;

    if (height > reqHeight) 
    {
        inSampleSize = Math.round((float)height / (float)reqHeight);
    }
    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) 
    {
        //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
        inSampleSize = Math.round((float)width / (float)reqWidth);
    }

    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
    }

答案 3 :(得分:1)

使用此代码可能会帮助您

Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    String path=Environment.getExternalStorageDirectory()+File.separator+Constants.APP_FOLDER_NAME+File.separator+Constants.ATTACHMENTS_FOLDER_NAME;
                    File mediapath=new File(path);
                    if(!mediapath.exists())
                    {
                        mediapath.mkdirs();
                    }
                    captured_image_uri=null;
                    captured_image_uri=Uri.fromFile(new File(mediapath.getPath(),"Image"+System.currentTimeMillis()+".jpg"));
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,captured_image_uri);
                    startActivityForResult(intent, Constants.PICK_FILE_FROM_CAMERA);

onActivityResult编写此代码

if(requestCode==Constants.PICK_FILE_FROM_CAMERA&&resultCode==getActivity().RESULT_OK)
            {
                try
                {
                    if(captured_image_uri!=null) {
                        ExifInterface exifInterface = new ExifInterface(captured_image_uri.getPath());
                        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                        Matrix matrix = new Matrix();
                        switch (orientation) {
                            case ExifInterface.ORIENTATION_ROTATE_90: {
                                matrix.postRotate(90);
                                break;
                            }
                            case ExifInterface.ORIENTATION_ROTATE_180: {
                                matrix.postRotate(180);
                                break;
                            }
                            case ExifInterface.ORIENTATION_ROTATE_270: {
                                matrix.postRotate(270);
                                break;
                            }
                        }
                        FileInputStream fis = new FileInputStream(captured_image_uri.getPath());
                        Bitmap bmp = BitmapFactory.decodeStream(fis);
                        Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                        FileOutputStream fos = new FileOutputStream(captured_image_uri.getPath());
                        rotated.compress(Bitmap.CompressFormat.JPEG, 85, fos);
                        uploadFileToServer(captured_image_uri.getPath());
                    }
                }catch (Exception e)
                {

                               e.printStackTrace();
                }

            }