当我捕获图像并在Android中保存到SD卡时图像被拉伸

时间:2017-03-29 06:48:47

标签: android

BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
bitmap = Bitmap.createScaledBitmap(bitmap, 150, 50, false);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = 150;
int newHeight = 50;

// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;

// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(-90);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
        width, height, matrix, true);
//      HomePage.image.setImageBitmap(resizedBitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Intent in1 = new Intent(CameraView.this, ImageDisplay.class);
in1.putExtra("picture",byteArray);
startActivity(in1);

这是我的代码。我无法设置图像,因为图像变得拉长。它不是全屏显示图像。我在哪里做错了?

我的图像是这样的,我们已修复它不应该拉伸。

enter image description here

4 个答案:

答案 0 :(得分:1)

这可能是由于'createScaledBitmap',因为图像尺寸很小,当你尝试调整它时,它会被拉伸

您可以使用:

   public Bitmap compressImage(String imageUri) {

        String filePath = getRealPathFromURI(imageUri);
        Bitmap scaledBitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();

//      by setting this field as true, the actual bitmap pixels are not loaded in the memory. Just the bounds are loaded. If
//      you try the use the bitmap here, you will get null.
        options.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(filePath, options);

        int actualHeight = options.outHeight;
        int actualWidth = options.outWidth;

//      max Height and width values of the compressed image is taken as 816x612

        float maxHeight = 816.0f;
        float maxWidth = 612.0f;
        float imgRatio = actualWidth / actualHeight;
        float maxRatio = maxWidth / maxHeight;

//      width and height values are set maintaining the aspect ratio of the image

        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if (imgRatio < maxRatio) {
                imgRatio = maxHeight / actualHeight;
                actualWidth = (int) (imgRatio * actualWidth);
                actualHeight = (int) maxHeight;
            } else if (imgRatio > maxRatio) {
                imgRatio = maxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) maxWidth;
            } else {
                actualHeight = (int) maxHeight;
                actualWidth = (int) maxWidth;

            }
        }

//      setting inSampleSize value allows to load a scaled down version of the original image

        options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);

//      inJustDecodeBounds set to false to load the actual bitmap
        options.inJustDecodeBounds = false;

//      this options allow android to claim the bitmap memory if it runs low on memory
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inTempStorage = new byte[16 * 1024];

        try {
//          load the bitmap from its path
            bmp = BitmapFactory.decodeFile(filePath, options);
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();

        }
        try {
            scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();
        }

        float ratioX = actualWidth / (float) options.outWidth;
        float ratioY = actualHeight / (float) options.outHeight;
        float middleX = actualWidth / 2.0f;
        float middleY = actualHeight / 2.0f;

        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

        Canvas canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

//      check the rotation of the image and display it properly
        ExifInterface exif;
        try {
            exif = new ExifInterface(filePath);

            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION, 0);
            Log.d("EXIF", "Exif: " + orientation);
            Matrix matrix = new Matrix();
            if (orientation == 6) {
                matrix.postRotate(90);
                Log.d("EXIF", "Exif: " + orientation);
            } else if (orientation == 3) {
                matrix.postRotate(180);
                Log.d("EXIF", "Exif: " + orientation);
            } else if (orientation == 8) {
                matrix.postRotate(270);
                Log.d("EXIF", "Exif: " + orientation);
            }
            scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                    scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
                    true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return scaledBitmap;

    }

答案 1 :(得分:0)

保存代码:

 FileOutputStream outStream = null;
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/camtest");
    dir.mkdirs();
    String fileName = String.format("%d.jpg", System.currentTimeMillis());
    File outFile = new File(dir, fileName);
    outStream = new FileOutputStream(outFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
    outStream.flush();
    outStream.close();
    refreshGallery(outFile);

权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

最后:

转到设备设置&gt;设备&gt;应用程序&gt;应用程序管理器&gt;“您的应用程序”&gt;权限&gt;启用存储权限!

答案 2 :(得分:0)

我有一个代码来从图库中选择图像或从相机中捕获图像,您可以使用它来实现您想要的效果,您将获得从相机捕获的文件路径或从onactivityforResult中的图库文件路径中选取

private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;

private static final int CROP_FROM_CAMERA = 2;
private static final int PICK_FROM_FILE = 3;


public static final int camPermissionRequestCode=6;
public static final int galleryPermissionRequestCode=4;

private Uri imageUri;

private File profilePicPicked;

@Override
public void onClick(View v) {


    int id=v.getId();

    switch (id){


        case R.id.camera:
            if (Build.VERSION.SDK_INT >= 23) {
                //do your check here

                isStoragePermissionGranted(camPermissionRequestCode);

            } else {

                captureImage();

            }

            break;
        case R.id.upload_image:

            if (Build.VERSION.SDK_INT >= 23) {
                //do your check here
                isStoragePermissionGranted(galleryPermissionRequestCode);

            } else {

                pickFromGallery();

            }


            break;

    }

}





@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub


    if (resultCode != Activity.RESULT_OK)
        return;



    switch (requestCode) {
        case CAMERA_CAPTURE_IMAGE_REQUEST_CODE:
            /**
             * After taking a picture, do the crop
             */


            File file2 = handleImageUri(imageUri,getContext());


            try {

                ExifInterface ei = new ExifInterface(file2.getAbsolutePath());
                int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_UNDEFINED);

                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        file2=rotateImage(file2, 90);
                       /* Toast.makeText(getActivity(),"90",Toast.LENGTH_SHORT).show();*/
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        file2=rotateImage(file2, 180);
                       /* Toast.makeText(getActivity(),"180",Toast.LENGTH_SHORT).show();*/
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        file2=rotateImage(file2, 270);
                       /* Toast.makeText(getActivity(),"270",Toast.LENGTH_SHORT).show();*/
                        break;
                    case ExifInterface.ORIENTATION_NORMAL:
                        /*Toast.makeText(getActivity(),"default",Toast.LENGTH_SHORT).show();*/
                    default:
                        break;

                }

            }catch (Exception e){

                e.printStackTrace();
            }


            profilePicPicked= file2;


            break;

        case PICK_FROM_FILE:
            /**
             * After selecting image from files, save the selected path
             */

            /*Toast.makeText(getContext(), "Image Taken", Toast.LENGTH_LONG).show();*/
            imageUri = data.getData();

            //iv_post.setImageURI(fileUri);
            File fileGAllr = getRealPathFromURIForGallery(getActivity(),imageUri);

            profilePicPicked=fileGAllr;


            // new Updata().execute("");
            // btn_gallery.setVisibility(View.GONE);

            break;

        case CROP_FROM_CAMERA:

            break;

    }

}



public static File getRealPathFromURIForGallery(Context context, Uri uri) {
    File file = null;
    try {


        String extension="";
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
        if (cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            extension= filePath.substring(filePath.lastIndexOf("."));
            System.out.print("");
        }
        cursor.close();

        InputStream input = null;
        input = context.getContentResolver().openInputStream(uri);

        if (input == null) {
            return null;
        }
        file = new File(context.getCacheDir(), System.currentTimeMillis()+"_Photo"+extension);
        OutputStream output = new FileOutputStream(file);
        try {
            byte[] buffer = new byte[4 * 1024];
            int read;
            while ((read = input.read(buffer)) != -1) {
                output.write(buffer, 0, read);
            }
            output.flush();
        } finally {
            output.close();
            input.close();
        }
    } catch (IOException e) {
        Log.d("Error", e.toString());
    }
    return file;
}




public File  rotateImage(File f, float angle) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap source = BitmapFactory.decodeFile(f.getAbsolutePath(), options);
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);


    Bitmap finalBm= Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

    File editedFile=getOutputMediaFile(MEDIA_TYPE_IMAGE);
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(editedFile);
        finalBm.compress(Bitmap.CompressFormat.JPEG, 100, out); // bmp is your Bitmap instance
        // PNG is a lossless format, the compression factor (100) is ignored
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    f.delete();

    return editedFile;
}


public static File handleImageUri(Uri uri, Context context) {
    Pattern pattern = Pattern.compile("(content://media/.*\\d)");
    Matcher matcher = pattern.matcher(uri.getPath());
    if (matcher.find()) {
        return getRealPathFromURI(context, uri);
    } else {
        return new File(uri.getPath());
    }
}


public static File getRealPathFromURI(Context context, Uri uri) {
    File file = null;
    try {
        InputStream input = null;
        input = context.getContentResolver().openInputStream(uri);

        if (input == null) {
            return null;
        }
        file = new File(context.getCacheDir(), System.currentTimeMillis()+"Photo.png");
        OutputStream output = new FileOutputStream(file);
        try {
            byte[] buffer = new byte[4 * 1024];
            int read;
            while ((read = input.read(buffer)) != -1) {
                output.write(buffer, 0, read);
            }
            output.flush();
        } finally {
            output.close();
            input.close();
        }
    } catch (IOException e) {
        Log.d("Error", e.toString());
    }
    return file;
}


public  boolean isStoragePermissionGranted(int requsetCode) {
    if (Build.VERSION.SDK_INT >= 23) {
        if (ContextCompat.checkSelfPermission(getActivity(),android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {

            if(requsetCode==camPermissionRequestCode){

                captureImage();
            }else{

                pickFromGallery();
            }
            return true;
        } else {


            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requsetCode);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation

        if(requsetCode==camPermissionRequestCode){

            captureImage();
        }else{

            pickFromGallery();
        }
        return true;
    }


}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){

        if(requestCode==camPermissionRequestCode){

            captureImage();
        }else if(requestCode==galleryPermissionRequestCode){

            pickFromGallery();
        }
        //resume tasks needing this permission
    }
}



private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    imageUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}


private void pickFromGallery(){

    Intent i = new Intent(
            Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, PICK_FROM_FILE);
}


public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}


private File getOutputMediaFile(int type) {
    // TODO Auto-generated method stub
    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "marcopolo");

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {

            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

答案 3 :(得分:0)

>       public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight){
>             final int height = options.outHeight;
>             final int width = options.outWidth;
>             int inSampleSize = 1;
>             if (height > reqHeight || width > reqWidth) {final int heightRatio = Math.round((float) height / (float) reqHeight);
>                 final int widthRatio = Math.round((float) width / (float) reqWidth);
>                 inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;}
>             final float totalPixels = width * height;
>             final float totalReqPixelsCap = reqWidth * reqHeight * 2;
>             while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {inSampleSize++;}
>             return inSampleSize;}
>     
>     > `Blockquote`