Captured Image Resolution is too big

后端 未结 2 1203
-上瘾入骨i
-上瘾入骨i 2021-01-28 21:51

What I am Doing ?

I am allowing user to capture image, storing it into SD Card and uploading to server.

But getting resolution of Captured image

相关标签:
2条回答
  • 2021-01-28 22:44

    Try below code , It may help you

    first convert your image with your required height and width

    public static Bitmap scaleImage(String p_path, int p_reqHeight, int p_reqWidth) throws Throwable
    {
        Bitmap m_bitMap = null;
        System.gc();
        File m_file = new File(p_path);
        if (m_file.exists())
        {
            BitmapFactory.Options m_bitMapFactoryOptions = new BitmapFactory.Options();
            m_bitMapFactoryOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
            m_bitMapFactoryOptions.inSampleSize = calculateInSampleSize(m_bitMapFactoryOptions, p_reqHeight, p_reqWidth);
            m_bitMapFactoryOptions.inJustDecodeBounds = false;
            m_bitMap = BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
        }
        else
        {
            throw new Throwable(p_path + " not found or not a valid image");
        }
        return m_bitMap;
    }
    
    
    private static int calculateInSampleSize(BitmapFactory.Options p_options, int p_reqWidth, int p_reqHeight)
    {
        // Raw height and width of image
        final int m_height = p_options.outHeight;
        final int m_width = p_options.outWidth;
        int m_inSampleSize = 1;
        if (m_height > p_reqHeight || m_width > p_reqWidth)
        {
            final int m_halfHeight = m_height / 2;
            final int m_halfWidth = m_width / 2;
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((m_halfHeight / m_inSampleSize) > p_reqHeight && (m_halfWidth / m_inSampleSize) > p_reqWidth)
            {
                m_inSampleSize *= 2;
            }
        }
        return m_inSampleSize;
    }
    
    0 讨论(0)
  • 2021-01-28 22:45

    use FileOutputStream.flush() after writing data...

    File file = new File(fileName.toString());
    try {
        FileOutputStream stream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, stream);
        stream.flush();
        stream.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
    

    You can save Bitmap image following code

    Bitmap photo = (Bitmap) "your Bitmap image";
    photo = Bitmap.createScaledBitmap(photo, 100, 100, false);
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
    
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "Imagename.jpg");
    f.createNewFile();
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
    fo.close();
    
    0 讨论(0)
提交回复
热议问题