电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Android 實現(xiàn)圖片轉(zhuǎn)二進制流及二進制轉(zhuǎn)字符串

瀏覽:81日期:2022-09-25 18:05:43

我就廢話不多說了,還是直接看代碼吧

public static byte[] readStream(String imagepath) throws Exception { FileInputStream fs = new FileInputStream(imagepath); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while (-1 != (len = fs.read(buffer))) { outStream.write(buffer, 0, len); } outStream.close(); fs.close(); return outStream.toByteArray();}// 二進制轉(zhuǎn)字符串public static String byte2hex(byte[] b){ StringBuffer sb = new StringBuffer(); String tmp = ''; for (int i = 0; i < b.length; i++) { tmp = Integer.toHexString(b[i] & 0XFF); if (tmp.length() == 1){ sb.append('0' + tmp); }else{ sb.append(tmp); } } return sb.toString();}

補充知識:圖片(bitmap)轉(zhuǎn)化為byte[],并上傳到服務(wù)器

壓縮為3:1bitmap

/** * 壓縮bitmap * * @param bitmap * @return */ private Bitmap compressBitmap(Bitmap bitmap) { // 創(chuàng)建操作圖片用的matrix對象 Matrix matrix = new Matrix(); matrix.postScale((float) 300 / bitmap.getWidth(),(float) 100 / bitmap.getHeight()); Log.e('', 'bitmap_width' + bitmap.getWidth()); Log.e('', 'bitmap_height' + bitmap.getHeight()); Bitmap compressBitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true); return compressBitmap; }

上傳壓縮后的bitmap

private void eleSignature() { WritePadDialog writeTabletDialog = new WritePadDialog(this, new DialogListener() { public void refreshActivity(Object object) {Bitmap mSignBitmap = (Bitmap) object;mSignBitmap = compressBitmap(mSignBitmap); //Bitmap newsrcImage = mSignBitmap.copy(Bitmap.Config.RGB_565, true);saveBmp(mSignBitmap);Log.e('', 'after bitmap_width:' + mSignBitmap.getWidth());Log.e('', 'after bitmap_height:' + mSignBitmap.getHeight());File file = new File(file_name);Log.e('Jeny', 'file.exists():' + file.exists());if (!file.exists()) { return;}byte[] eleSignatureData = null;try { eleSignatureData = InputStreamTOByte();} catch (IOException e) { }Log.e('Jeny', 'eleSignatureData:' + eleSignatureData.length);if (eleSignatureData == null) { return;} //byte[] eleSignatureData = InputStreamTOByte();//byte[] eleSignatureData = Bitmap2Bytes(newsrcImage);mController.startSavingElecSignatureDataOfSDPay(SalesCollectActivity.this, voucherNo, eleSignatureData);mSignBitmap.recycle(); } } ); writeTabletDialog.setCanceledOnTouchOutside(false); writeTabletDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {if (keyCode == KeyEvent.KEYCODE_BACK) { return true;} else { return false; //默認返回 false} } }); writeTabletDialog.show(); } /** * 將InputStream轉(zhuǎn)換成byte數(shù)組 * <p/> * InputStream * * @return byte[] * @throws IOException */ @SuppressWarnings('resource') public byte[] InputStreamTOByte() throws IOException { File file = new File(file_name); if (!file.exists()) { return null; } FileInputStream is = new FileInputStream(file_name); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[BUFFER_SIZE]; int count = -1; while ((count = is.read(data, 0, BUFFER_SIZE)) != -1) outStream.write(data, 0, count); // data = null; byte[] alldata = outStream.toByteArray(); /**top = new byte[62]; for (int i = 0; i < 62; i++) { top[i] = alldata[i]; } writefile(top_name, top); */// byte[] other = new byte[alldata.length - 62];// for (int i = 0; i < (alldata.length - 62); i++)// {// other[i] = alldata[i + 62];// } /* * String b = * Environment.getExternalStorageDirectory().getPath()+'/other.txt'; * writefile(b, other); */ return alldata; } /** * @param bitmap 圖片以bmp格式存儲的方法 */ private void saveBmp(Bitmap bitmap) { if (bitmap == null) return; // 位圖大小 int nBmpWidth = bitmap.getWidth(); int nBmpHeight = bitmap.getHeight(); int DataSizePerLine = 40; // 圖像數(shù)據(jù)大小 int bufferSize = nBmpHeight * DataSizePerLine; try { // 存儲文件名 File file = new File(file_name); Log.i('SurFace', 'filename:' + file_name); if (!file.exists()) {file.createNewFile(); } FileOutputStream fileos = new FileOutputStream(file_name); // bmp文件頭 int bfType = 0x4d42; long bfSize = 14 + 40 + 8 + bufferSize; int bfReserved1 = 0; int bfReserved2 = 0; long bfOffBits = 14 + 40 + 8; // 保存bmp文件頭 writeWord(fileos, bfType); writeDword(fileos, bfSize); writeWord(fileos, bfReserved1); writeWord(fileos, bfReserved2); writeDword(fileos, bfOffBits); // bmp信息頭 long biSize = 40L; long biWidth = nBmpWidth; long biHeight = nBmpHeight; int biPlanes = 1; int biBitCount = 1; long biCompression = 0L; long biSizeImage = 0L; long biXpelsPerMeter = 0L; long biYPelsPerMeter = 0L; long biClrUsed = 0L; long biClrImportant = 0L; // 保存bmp信息頭 writeDword(fileos, biSize); writeLong(fileos, biWidth); writeLong(fileos, biHeight); writeWord(fileos, biPlanes); writeWord(fileos, biBitCount); writeDword(fileos, biCompression); writeDword(fileos, biSizeImage); writeLong(fileos, biXpelsPerMeter); writeLong(fileos, biYPelsPerMeter); writeDword(fileos, biClrUsed); writeDword(fileos, biClrImportant); // 調(diào)色板 byte[] c = new byte[4]; c[0] = 0; c[1] = 0; c[2] = 0; c[3] = 0; fileos.write(c); byte[] w = new byte[4]; w[0] = (byte) 255; w[1] = (byte) 255; w[2] = (byte) 255; w[3] = 0; fileos.write(w); int wWidth = DataSizePerLine; // 像素掃描 byte bmpData[] = new byte[bufferSize]; for (int ibuf = 0; ibuf < bufferSize; ibuf++)bmpData[ibuf] = (byte) 0xff; int iByte = 0; int iCount = 0; byte bPix = 0; for (int nCol = nBmpHeight - 1, nRealCol = nBmpHeight; nCol > 0; nCol--, nRealCol--) {wWidth = DataSizePerLine;iCount = 0;iByte = 0;for (int wRow = 0; wRow < nBmpWidth; wRow++) { int clr = bitmap.getPixel(wRow, nCol); int r = (clr | 0xff00ffff) >> 16 & 0x00ff; int g = Color.green(clr); int b = Color.blue(clr); // byte a = (byte) Color.blue(clr); // byte b = (byte) Color.green(clr); // byte d = (byte) Color.red(clr); // System.out.println('r:'+r); if (r != 255 || g != 255 || b != 255)// 黑色 { // bPix = (byte)(bPix & (0x7f>>iByte | // 0x7f<<(8-iByte))); bPix = (byte) (bPix & ~(byte) Math.pow(2, iByte)); // System.out.println( ' 黑色 ' + bPix ); } else { // bPix = (byte)(bPix & (0xff>>iByte | // 0xff<<(8-iByte))); bPix = (byte) (bPix | (byte) Math.pow(2, iByte)); // System.out.println( ' 白色 ' + bPix); } if (iCount != 0 && iCount % 8 == 0) { bmpData[nRealCol * wWidth - wRow / 8] = bPix; iByte = 0; } else { iByte++; } iCount++; /* * //if(a==b && b==d && a>0) if (nRealCol%2 == 0) { //為白色 * bmpData[nRealCol * wWidth] = (byte)255; }else{ //為黑色 * bmpData[nRealCol * wWidth] = 0; } */ // Arrays.fill(x, value); /* * bmpData[nRealCol * wWidth + wByteIdex] = (byte) Color .b; * bmpData[nRealCol * wWidth + wByteIdex + 1] = (byte) Color * .green(clr); bmpData[nRealCol * wWidth + wByteIdex + 2] = * (byte) Color .red(clr); */} } // System.out.println( ' iCount= ' + iCount); /* * byte[] writeByte = new byte[bmpData.length]; for(int * i=0;i<writeByte.length;i++){ writeByte[i] = * bmpData[bmpData.length-i-1]; } */ fileos.write(bmpData); fileos.flush(); fileos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void writeWord(FileOutputStream stream, int value) throws IOException { byte[] b = new byte[2]; b[0] = (byte) (value & 0xff); b[1] = (byte) (value >> 8 & 0xff); stream.write(b); } protected void writeDword(FileOutputStream stream, long value) throws IOException { byte[] b = new byte[4]; b[0] = (byte) (value & 0xff); b[1] = (byte) (value >> 8 & 0xff); b[2] = (byte) (value >> 16 & 0xff); b[3] = (byte) (value >> 24 & 0xff); stream.write(b); } protected void writeLong(FileOutputStream stream, long value) throws IOException { byte[] b = new byte[4]; b[0] = (byte) (value & 0xff); b[1] = (byte) (value >> 8 & 0xff); b[2] = (byte) (value >> 16 & 0xff); b[3] = (byte) (value >> 24 & 0xff); stream.write(b); } /** * 壓縮bitmap * * @param bitmap * @return */ private Bitmap compressBitmap(Bitmap bitmap) { // 創(chuàng)建操作圖片用的matrix對象 Matrix matrix = new Matrix(); matrix.postScale((float) 300 / bitmap.getWidth(),(float) 100 / bitmap.getHeight()); Log.e('', 'bitmap_width' + bitmap.getWidth()); Log.e('', 'bitmap_height' + bitmap.getHeight()); Bitmap compressBitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true); return compressBitmap; }

以上這篇Android 實現(xiàn)圖片轉(zhuǎn)二進制流及二進制轉(zhuǎn)字符串就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標簽: Android
相關(guān)文章:
主站蜘蛛池模板: 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 高温链条油|高温润滑脂|轴承润滑脂|机器人保养用油|干膜润滑剂-东莞卓越化学 | 模具钢_高速钢_不锈钢-万利钢金属材料 | 杭州代理记账多少钱-注册公司代办-公司注销流程及费用-杭州福道财务管理咨询有限公司 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 | 电磁铁_小型推拉电磁铁_电磁阀厂家-深圳市宗泰电机有限公司 | 温州在线网 | 中央空调温控器_风机盘管温控器_智能_液晶_三速开关面板-中央空调温控器厂家 | 泰国试管婴儿_泰国第三代试管婴儿费用|成功率|医院—新生代海外医疗 | 【灵硕展览集团】展台展会设计_展览会展台搭建_展览展示设计一站式服务公司 | 萃取箱-萃取槽-PVC萃取箱厂家-混合澄清槽- 杭州南方化工设备 | 桂林腻子粉_内墙外墙抗裂砂浆腻子粉推荐广西鑫达涂料厂家供应 | 精益专家 - 设备管理软件|HSE管理系统|设备管理系统|EHS安全管理系统 | 冷镦机-多工位冷镦机-高速冷镦机厂家-温州金诺机械设备制造有限公司 | 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 自动气象站_气象站监测设备_全自动气象站设备_雨量监测站-山东风途物联网 | 线粒体膜电位荧光探针-细胞膜-标记二抗-上海复申生物科技有限公司 | 包头市鑫枫装饰有限公司| 密集架-手摇-智能-移动-价格_内蒙古档案密集架生产厂家 | 煤机配件厂家_刮板机配件_链轮轴组_河南双志机械设备有限公司 | 菲希尔FISCHER测厚仪-铁素体检测仪-上海吉馨实业发展有限公司 | 对照品_中药对照品_标准品_对照药材_「格利普」高纯中药标准品厂家-成都格利普生物科技有限公司 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 大巴租车平台承接包车,通勤班车,巴士租赁业务 - 鸿鸣巴士 | 上海租车公司_上海包车_奔驰租赁_上海商务租车_上海谐焕租车 | 防渗土工膜|污水处理防渗膜|垃圾填埋场防渗膜-泰安佳路通工程材料有限公司 | 【电子厂招聘_普工招工网_工厂招聘信息平台】-工立方打工网 | 编织人生 - 权威手工编织网站,编织爱好者学习毛衣编织的门户网站,织毛衣就上编织人生网-编织人生 | 出国劳务公司_正规派遣公司[严海] | 北京律师事务所_房屋拆迁律师_24小时免费法律咨询_云合专业律师网 | 主题班会网 - 安全教育主题班会,各类主题班会PPT模板 | 二手电脑回收_二手打印机回收_二手复印机回_硒鼓墨盒回收-广州益美二手电脑回收公司 | 步进_伺服_行星减速机,微型直流电机,大功率直流电机-淄博冠意传动机械 | 上海小程序开发-上海小程序制作公司-上海网站建设-公众号开发运营-软件外包公司-咏熠科技 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 亳州网络公司 - 亳州网站制作 - 亳州网站建设 - 亳州易天科技 | 生物除臭剂-除味剂-植物-污水除臭剂厂家-携葵环保有限公司 | 流变仪-热分析联用仪-热膨胀仪厂家-耐驰科学仪器商贸 | 植筋胶-粘钢胶-碳纤维布-碳纤维板-环氧砂浆-加固材料生产厂家-上海巧力建筑科技有限公司 | 聚合氯化铝厂家-聚合氯化铝铁价格-河南洁康环保科技 | 户外环保不锈钢垃圾桶_标识标牌制作_园林公园椅厂家_花箱定制-北京汇众环艺 | 下水道疏通_管道疏通_马桶疏通_附近疏通电话- 立刻通 |