android将照片压缩并显示,android拍照选择图库后将照片剪裁压缩显示到imageview上 -电脑资料...
调用图库和拍照的时候权限:
java 代码:
public class KujieSY extends Activity implements OnClickListener{
/** (非 Javadoc)
* 方法名: onCreate
* 描述:
* 作者 vencent
* @param savedInstanceState
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
private TextView backs;
private ImageView images;
String takephoto="";
File PicFile;
KujieSY self=KujieSY.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.kuaijieshouyin);
initView();
}
public void initView(){
backs=(TextView)findViewById(R.id.backs);
images=(ImageView)findViewById(R.id.images);
backs.setOnClickListener(this);
images.setOnClickListener(this);
}
/** (非 Javadoc)
* 方法名: onClick
* 描述:
* 作者 vencent
* @param arg0
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.backs:
finish();
break;
case R.id.images:
new AlertDialog.Builder(this)
.setTitle("选择传图方式")
.setIcon(R.drawable.logoyy)
.setPositiveButton("图库选择",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, 1);
}
}).setNegativeButton("拍照", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
takephoto=savepic();
Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(PicFile));
imageCaptureIntent.putExtra("android.intent.extra.videoQuality", 0);
startActivityForResult(imageCaptureIntent, 2);
}
}).show();
break;
}
}
public String savepic(){
File files=new File(IMP.saveimages);
if(!files.exists()){
files.mkdirs();
}
String picpath=IMP.saveimages+"/"+System.currentTimeMillis()+".jpg";
PicFile=new File(picpath);
return picpath;
}
/** (非 Javadoc)
* 方法名: onActivityResult
* 描述:
* 作者 vencent
* @param requestCode
* @param resultCode
* @param data
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==0){
return;
}
switch (requestCode) {
case 1:
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
//Bitmap bitmap=getLoacalBitmap(data.getDataString());
String newfileselect=IMP.saveimages+"/"+System.currentTimeMillis()+".jpg";
ImageCrop.Crop(self, picturePath, newfileselect);
images.setImageBitmap(BitmapFactory.decodeFile(newfileselect));
break;
case 2:
String newfile=IMP.saveimages+"/"+System.currentTimeMillis()+".jpg";
ImageCrop.Crop(self, PicFile.getAbsolutePath(), newfile);
images.setImageBitmap(BitmapFactory.decodeFile(newfile));
break;
}
}
/**
*
* @方法名: startPhotoZoom
* @描述: TODO(裁剪照片)
* @作者 张丽平
* @参数 @param uri
* @返回值 void
* @throws
*/
public void startPhotoZoom(Uri uri){
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// // outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 128);
intent.putExtra("outputY", 128);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
}
}
压缩图片:
package com.jfy.app;
import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import org.apache.http.conn.ClientConnectionManager;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.net.Uri;
public class ImageCrop {
public static boolean Crop(Context context, String pathName,
String newPathName) {
return Crop(context, pathName, newPathName, 1024);
}
public static boolean Crop(Context context, String pathName,
String newPathName, int maxXY) {
File fielName = new File(pathName);
File newFielName = new File(newPathName);
File newParentFile = newFielName.getParentFile();
if (!fielName.exists()) {
return false;
}
if (!newParentFile.exists()) {
newParentFile.mkdirs();
}
try {
Bitmap mBitmap = createFromUri(context, Uri.fromFile(fielName)
.toString(), 2 * maxXY, 2 * maxXY, 0, null);
if (mBitmap == null) {
return false;
}
Bitmap b = null;
if (mBitmap.getWidth() > mBitmap.getHeight()
&& mBitmap.getWidth() > maxXY) {
int tempHiget = maxXY * mBitmap.getHeight()
/ mBitmap.getWidth();
b = ThumbnailUtils.extractThumbnail(mBitmap, maxXY, tempHiget);
} else if (mBitmap.getHeight() > mBitmap.getWidth()
&& mBitmap.getHeight() > maxXY) {
int tempWidth = maxXY * mBitmap.getWidth()
/ mBitmap.getHeight();
b = ThumbnailUtils.extractThumbnail(mBitmap, tempWidth, maxXY);
}
if (b != null && mBitmap != b) {
mBitmap.recycle();
mBitmap = b;
}
saveOutput(context, mBitmap, newFielName);
return true;
} catch (Exception e) {
if (newFielName.exists()) {
newFielName.delete();
}
}
return false;
}
/**
* 从文件或Uri获得Bitmap 限制像素大小(maxResolutionX*maxResolutionY)
* 限制最小边长min(maxResolutionX*maxResolutionY)/2
*/
private static final Bitmap createFromUri(Context context, String uri,
int maxResolutionX, int maxResolutionY, long cacheId,
ClientConnectionManager connectionManager) throws IOException,
URISyntaxException, OutOfMemoryError {
final BitmapFactory.Options ptions = new BitmapFactory.Options();
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;
Bitmap bitmap = null;
// Get the input stream for computing the sample size.
BufferedInputStream bufferedInput = null;
if (uri.startsWith(ContentResolver.SCHEME_CONTENT)
|| uri.startsWith(ContentResolver.SCHEME_FILE)) {
// Get the stream from a local file.
bufferedInput = new BufferedInputStream(context
.getContentResolver().openInputStream(Uri.parse(uri)),
16384);
} else {
return null;
}
// Compute the sample size, i.e., not decoding real pixels.
if (bufferedInput != null) {
options.inSampleSize = computeSampleSize(bufferedInput,
maxResolutionX, maxResolutionY);
} else {
return null;
}
// Get the input stream again for decoding it to a bitmap.
bufferedInput = null;
if (uri.startsWith(ContentResolver.SCHEME_CONTENT)
|| uri.startsWith(ContentResolver.SCHEME_FILE)) {
// Get the stream from a local file.
bufferedInput = new BufferedInputStream(context
.getContentResolver().openInputStream(Uri.parse(uri)),
16384);
} else {
return null;
}
// Decode bufferedInput to a bitmap.
if (bufferedInput != null) {
options.inDither = false;
options.inJustDecodeBounds = false;
Thread timeoutThread = new Thread("BitmapTimeoutThread") {
public void run() {
try {
Thread.sleep(6000);
options.requestCancelDecode();
} catch (InterruptedException e) {
}
}
};
timeoutThread.start();
bitmap = BitmapFactory.decodeStream(bufferedInput, null, options);
closeSilently(bufferedInput);
}
return bitmap;
}
/** 计算SampleSize【1】 */
private static int computeSampleSize(InputStream stream,
int maxResolutionX, int maxResolutionY) {
BitmapFactory.Options ptions = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(stream, null, options);
int maxNumOfPixels = maxResolutionX * maxResolutionY;
int minSideLength = Math.min(maxResolutionX, maxResolutionY) / 2;
return UtilscomputeSampleSize(options, minSideLength, maxNumOfPixels);
}
/** 计算SampleSize【2】 */
private static int UtilscomputeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
/** 计算SampleSize【3】 */
private static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
final int UNCONSTRAINED = -1;
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 : (int) Math
.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == UNCONSTRAINED) ? 128 : (int) Math
.min(Math.floor(w / minSideLength),
Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == UNCONSTRAINED)
&& (minSideLength == UNCONSTRAINED)) {
return 1;
} else if (minSideLength == UNCONSTRAINED) {
return lowerBound;
} else {
return upperBound;
}
}
/** 保存Bitmap到Uri地址【content:// or file://;"image/*" or "JPEG"】 */
private static void saveOutput(Context context, Bitmap mBitmap,
File newFielName) {
OutputStream utputStream = null;
try {
utputStream = context.getContentResolver().openOutputStream(
Uri.fromFile(newFielName));
if (outputStream != null) {
mBitmap.compress(Bitmap.CompressFormat.valueOf("JPEG"), 85,
outputStream);
}
} catch (IOException ex) {
} finally {
closeSilently(outputStream);
if (mBitmap != null) {
mBitmap.recycle();
mBitmap = null;
}
}
}
/** 关闭流文件 */
private static void closeSilently(Closeable c) {
if (c == null)
return;
try {
c.close();
} catch (Throwable t) {
}
}
}
xml 代码:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#006dba"
android:padding="5dip" >
android:id="@+id/backs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/back"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:paddingRight="20dip"
android:paddingTop="5dip" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:text="快捷收银"
android:textColor="#ffffff"
android:textSize="20sp" />
android:layout_width="match_parent"
android:layout_height="match_parent"
>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip"
>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dip"
android:orientation="horizontal"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商户名称:"
android:textColor="#000000"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/shanghuname"
android:background="@drawable/xian"
/>
android:layout_width="100dip"
android:layout_height="100dip"
android:id="@+id/images"
android:scaleType="centerInside"
android:src="@drawable/chongzhi"
/>
android将照片压缩并显示,android拍照选择图库后将照片剪裁压缩显示到imageview上 -电脑资料...相关推荐
- 电脑显示苹果5s未连接服务器怎么办啊,iphone5s数据线连接不上电脑怎么办【解决方法】...
iphone5s手机在购买的时候配有数据线,这款数据线是有传输功能和充电两种功能的,但是有的朋友在使用这手机连接电脑的时候会发现没有办法与电脑连接?由此而感到疑惑不解,应该如何解决呢? 1. 苹果 5 ...
- u盘正常接入后计算机无法看到,u盘插上电脑后不能正常显示U盘怎么办
昨天这个U盘使用还是正常的,可是今天将U盘插入电脑后就完全没反应了,相信还有其它人遇到过类似的情况,经过一番折腾终于解决U盘不显示的问题,u盘插上电脑后没反应,不能正常显示U盘怎么办?现在学习啦小编就 ...
- QT5显示视频或者图片缩小后以滚动条方式显示
今天在QT里将视频显示在停靠窗口里,并且能够缩小后,出现滑动条,放大到一定阶段则不再放大. 下面是代码: dockwindows.h #ifndef DOCKWINDOWS_H #define DOC ...
- 剑网3选择人物后进入游戏就显示服务器断开连接,剑网3怎么进入游戏_剑网3进入游戏的问题_牛游戏网...
安装好游戏之后,点击桌面游戏图标运行<剑网3>游戏更新程序.如果游戏需要更新,游戏更新程序将自动更新至最新版本.您也可以选择下载补丁包手动更新游戏. 游戏更新程序上可以快速注册帐号.付费充 ...
- Android 通过图片资源名字获得图片资源设置在ImageView上
Android 通过图片资源名字获得图片资源设置在ImageView上 操作 案例 操作 如何通过图片资源名得到图片资源? // 拿到图片名字 String iconName = "weat ...
- 无线网卡安装后显示无服务器,USB无线网卡安装后连接图标不显示怎么办【解决方法】...
USB无线网卡安装后右下角连接图标不显示怎么办? 无线网卡的作用.功能跟普通电脑网卡一样,是用来连接到局域网上的.它只是一个信号收发的设备,只有在找到上互联网的出口时才能实现与互联网的连接,所有无线网 ...
- Android实现仿微信朋友圈发布动态(拍照、图库选择、照片压缩、显示、保存、缩略图、点击缩略图删除对应文件等)
原址: http://blog.csdn.net/zhang3776813/article/details/52092591 /*** 仿微信朋友圈发布动态* 拍照或图库选择 * 压缩图片并保存**/ ...
- Android实现仿微信朋友圈发布动态(拍照、图库选择、照片压缩、显示、保存、缩略图、点击缩略图删除对应文件等)附源码
原创作品,转载请注明出处:http://blog.csdn.net/zhang3776813/article/details/52092591 最近项目需求中要用到类似微信朋友圈发布 ...
- android 拍照头像,Android 拍照/选择照片并剪切成头像
头像裁剪上传功能在现在的App中基本都要用到,今天总结了一下,用比较简单实用的方式(调用系统API)来实现需求.这样做简单,但是不同的手机和系统上裁剪和选择图片的方式有一些区别. 首先来看一下效果图 ...
- Android APP Camera2应用(03)拍照保存照片流程
说明:camera子系统 系列文章针对Android10.0系统,主要针对 camera API2 + HAL3 框架进行解读. 1 拍照&保存照片流程简要解读 在完成预览操作之后,点击拍照按 ...
最新文章
- Shell中的${ }、#、##、%、%%使用范例
- ASP.NET 完成基于表单的身份验证
- chapter1:The way of the program
- 华为鸿蒙适配计划,华为鸿蒙适配计划提前曝光,快看看有你的机型吗?
- bert中文预训练模型_[中文医疗预训练模型] MC-BERT
- 重提URL Rewrite(3):在URL Rewrite后保持PostBack地址(转老赵blog)
- 省吃俭用 ,究竟祸害了多少人 ?
- 与WebXR共同创建者Diego Marcos一起探讨沉浸式Web的未来(下)
- C# 联合查询_c# 之linq——小白入门级
- 【报告分享】2020巨量引擎营销通案.pdf(附下载链接)
- 服务器虚拟化nas存储服务器搭建,采用NAS架构优化VMware服务器虚拟化环境的存储管理.pdf...
- Silverlight:Dependency Property(依赖属性)学习笔记
- php获取类的实例变量
- 评价指标MSE和AUC的参考文献
- ppt怎么把图片做成翻书效果_怎么把在PPT中插入的图片效果弄成翻书的样子啊?...
- iPhone4 兼容 iPhone 5 需要注意的地方
- apdl与传统计算机语言,ANSYS经典APDL语言详解及ANSYS二次开发
- Photoshop CS2 视频教程-PS制作霓虹灯效果(转)
- Java通过axis调用WebService
- 高通挥刀 | 一点财经