How to Take a Photo with the Camera App And also save as thumbnail and as Full-size Photo (resize & crop customly) in SD Card Two Folders?

蓝咒 提交于 2019-12-25 02:47:02

问题


I am new in android development. I want take an image from android camera tool then save as thumbnail and also save as Full-size (Resize & crop user defined).

I actually want thumbnail image show in GridView from one folder of SD Card then click one image then show full-size(1536 X 2048) image in one window from another folder of SD Card.

I search in google but not get absolute example or tutorial which is match two conditions.

For example

developer.android.com/training/camera/photobasics.html#TaskPath

taking pictures with camera android programmatically

Capture Image from Camera and Display in Activity

Please anybody help me, how to possible this. If my concept mismatch then modify to update my question. Please anybody help me.


回答1:


Follow bellow Steps:-

1:-

private  Bitmap getScaledBitMap(String filePath, int reqWidth, int reqHeight) {

if (filePath != null && filePath.contains("file")) {
    filePath = filePath.replace("file://", "");
}
Bitmap unscaledBitmap = ScalingUtilities.decodeBitmap(getResources(), filePath, reqWidth, reqHeight, ScalingLogic.CROP);
// Part 2: Scale image
Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, reqWidth,reqHeight, ScalingLogic.CROP);
unscaledBitmap.recycle();

return scaledBitmap;

}

2:-

public class ScalingUtilities {

/**
 * Utility function for decoding an image resource. The decoded bitmap will
 * be optimized for further scaling to the requested destination dimensions
 * and scaling logic.
 *
 * @param res The resources object containing the image data
 * @param resId The resource id of the image data
 * @param dstWidth Width of destination area
 * @param dstHeight Height of destination area
 * @param scalingLogic Logic to use to avoid image stretching
 * @return Decoded bitmap
 */
public static Bitmap decodeBitmap(Resources res, String pathName, int dstWidth, int dstHeight,
        ScalingLogic scalingLogic) {
    Options options = new Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);
    options.inJustDecodeBounds = false;
    options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth,
            dstHeight, scalingLogic);
    Bitmap unscaledBitmap = BitmapFactory.decodeFile(pathName, options);

    return unscaledBitmap;
}

/**
 * Utility function for creating a scaled version of an existing bitmap
 *
 * @param unscaledBitmap Bitmap to scale
 * @param dstWidth Wanted width of destination bitmap
 * @param dstHeight Wanted height of destination bitmap
 * @param scalingLogic Logic to use to avoid image stretching
 * @return New scaled bitmap object
 */
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight,
        ScalingLogic scalingLogic) {
    Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
            dstWidth, dstHeight, scalingLogic);
    Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
            dstWidth, dstHeight, scalingLogic);
    Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(),
            Config.ARGB_8888);
    Canvas canvas = new Canvas(scaledBitmap);
    canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;
}

/**
 * ScalingLogic defines how scaling should be carried out if source and
 * destination image has different aspect ratio.
 *
 * CROP: Scales the image the minimum amount while making sure that at least
 * one of the two dimensions fit inside the requested destination area.
 * Parts of the source image will be cropped to realize this.
 *
 * FIT: Scales the image the minimum amount while making sure both
 * dimensions fit inside the requested destination area. The resulting
 * destination dimensions might be adjusted to a smaller size than
 * requested.
 */
public static enum ScalingLogic {
    CROP, FIT
}

/**
 * Calculate optimal down-sampling factor given the dimensions of a source
 * image, the dimensions of a destination area and a scaling logic.
 *
 * @param srcWidth Width of source image
 * @param srcHeight Height of source image
 * @param dstWidth Width of destination area
 * @param dstHeight Height of destination area
 * @param scalingLogic Logic to use to avoid image stretching
 * @return Optimal down scaling sample size for decoding
 */
public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight,
        ScalingLogic scalingLogic) {
    if (scalingLogic == ScalingLogic.FIT) {
        final float srcAspect = (float)srcWidth / (float)srcHeight;
        final float dstAspect = (float)dstWidth / (float)dstHeight;

        if (srcAspect > dstAspect) {
            return srcWidth / dstWidth;
        } else {
            return srcHeight / dstHeight;
        }
    } else {
        final float srcAspect = (float)srcWidth / (float)srcHeight;
        final float dstAspect = (float)dstWidth / (float)dstHeight;

        if (srcAspect > dstAspect) {
            return srcHeight / dstHeight;
        } else {
            return srcWidth / dstWidth;
        }
    }
}

/**
 * Calculates source rectangle for scaling bitmap
 *
 * @param srcWidth Width of source image
 * @param srcHeight Height of source image
 * @param dstWidth Width of destination area
 * @param dstHeight Height of destination area
 * @param scalingLogic Logic to use to avoid image stretching
 * @return Optimal source rectangle
 */
public static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight,
        ScalingLogic scalingLogic) {
    if (scalingLogic == ScalingLogic.CROP) {
        final float srcAspect = (float)srcWidth / (float)srcHeight;
        final float dstAspect = (float)dstWidth / (float)dstHeight;

        if (srcAspect > dstAspect) {
            final int srcRectWidth = (int)(srcHeight * dstAspect);
            final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
            return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight);
        } else {
            final int srcRectHeight = (int)(srcWidth / dstAspect);
            final int scrRectTop = (int)(srcHeight - srcRectHeight) / 2;
            return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight);
        }
    } else {
        return new Rect(0, 0, srcWidth, srcHeight);
    }
}

/**
 * Calculates destination rectangle for scaling bitmap
 *
 * @param srcWidth Width of source image
 * @param srcHeight Height of source image
 * @param dstWidth Width of destination area
 * @param dstHeight Height of destination area
 * @param scalingLogic Logic to use to avoid image stretching
 * @return Optimal destination rectangle
 */
public static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight,
        ScalingLogic scalingLogic) {
    if (scalingLogic == ScalingLogic.FIT) {
        final float srcAspect = (float)srcWidth / (float)srcHeight;
        final float dstAspect = (float)dstWidth / (float)dstHeight;

        if (srcAspect > dstAspect) {
            return new Rect(0, 0, dstWidth, (int)(dstWidth / srcAspect));
        } else {
            return new Rect(0, 0, (int)(dstHeight * srcAspect), dstHeight);
        }
    } else {
        return new Rect(0, 0, dstWidth, dstHeight);
    }
}

}

save this bitmap into required folder.




回答2:


Create a File

private final int CAMERA_PIC_REQUEST=0;

 private String createNewFile()
{

 String dirctory = Environment.getExternalStorageDirectory()+"/"+"test";
    File f = new File(dirctory);
    boolean isdirectory;
    if(!f.isDirectory())
    {
        isdirectory = f.mkdir();
    }else
    {
        isdirectory = true;
    }

    if(isdirectory)
    {
        dirctory  = dirctory +"/"+Calendar.getInstance().getTimeInMillis()+"captureImage.jpg";
        f = new File(dirctory);
        if(f.isFile())
            f.delete();
        try {
            f.createNewFile();

            return dirctory;
        } catch (Exception e) {
            Utility.Toast(mActivity, "Unable to create File");
            e.printStackTrace();
        }
    }else
    {
        Utiity.Toast(AgentSignUp.this, "Unable to create Directory");
    }
    return dirctory;
}

Step 2: fire intent:

             Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            file_image = createNewFile();
            if(file_image!=null)
            {
                File file = new File(file_image);
                Uri outputFileUri = Uri.fromFile( file );
                cameraIntent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
            }
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

step 3: recieve in onActivity Result (check for sucess and failure)

                @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode==RESULT_OK)
    {
        if(requestCode==CAMERA_PIC_REQUEST)
        {
            File file = new File(file_image);
            Uri outputFileUri = Uri.fromFile( file );
            doCrop(outputFileUri);// send to cropper or you can use this uri for upper code to get two size of images
        }
}
super.onActivityResult(requestCode, resultCode, data);
}


来源:https://stackoverflow.com/questions/23494419/how-to-take-a-photo-with-the-camera-app-and-also-save-as-thumbnail-and-as-full-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!