Bitmap的一些操作

来源:互联网 发布:sql验证身份证号码 编辑:程序博客网 时间:2024/06/07 04:53
/**
     * 旋转图片
     *
     * @param bmp
     * @param degrees
     * @return
     */
    public Bitmap rotateIMG(Bitmap bmp, float degrees) {
        Bitmap bitmap = null;

        Matrix matrix = new Matrix();
        matrix.setRotate(degrees);
        bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
                bmp.getHeight(), matrix, true);

        return bitmap;

    }


/* 压缩到指定宽高 */
    private Bitmap getBitmapFromFile(String pathName, int width, int height) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        Bitmap src = BitmapFactory.decodeFile(pathName, opts);
        opts.outWidth = width;
        opts.outHeight = height;
        opts.inJustDecodeBounds = false;
        src = BitmapFactory.decodeFile(pathName, opts);
        Toast.makeText(this, src.getWidth() + " " + src.getHeight(),
                Toast.LENGTH_SHORT).show();

        int w = src.getWidth();
        int h = src.getHeight();
        float scaleWidth = ((float) width) / w;
        float scaleHeight = ((float) height) / h;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap bmp = Bitmap.createBitmap(src, 0, 0, w, h, matrix, true);
        Toast.makeText(this, bmp.getWidth() + " " + bmp.getHeight(),
                Toast.LENGTH_SHORT).show();

        return bmp;
    }



/**
     * 得到文件路径
     *
     * @return
     */
    private File getFile() {
        File file = new File("/sdcard/epd/sc.txt");
        try {
            boolean sdCardExist = Environment.getExternalStorageState().equals(
                    android.os.Environment.MEDIA_MOUNTED);
            if (sdCardExist) {
                String fileName = "epd";
                String sdPath = Environment.getExternalStorageDirectory()
                        .getPath();
                File tempFile = new File(sdPath + File.separator + fileName);
                if (!tempFile.exists()) {
                    tempFile.mkdirs();
                }
                file = new File(sdPath + File.separator + fileName
                        + File.separator + "sc" + ".txt");
                /*
                 * if (file.exists()) { file.delete(); file.createNewFile(); }
                 */
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }


/*文件保存*/

FileOutputStream fos = null;
        try {
            File file = getFile();
            fos = new FileOutputStream(file, false);
            fos.write(buffer);
            fos.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


/**
     * time:2015-07-06
     *  读取文件
     * @param path
     * @return
     */
    private byte[] getImageBytes() {
        byte[] buffer = null;
        File file = getFile();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            int lenght = fis.available();
            buffer = new byte[lenght];
            fis.read(buffer);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return buffer;
    }

0 0
原创粉丝点击