android的PhotoShop
来源:互联网 发布:对网络教育行业的看法 编辑:程序博客网 时间:2024/06/10 00:34
不说废话,相信大家可以看懂
/**
* 图片处理类* @author teng
*
*/
public class Photoshop {
/**
* 设置图片的大小
* @param src
* @param weight
* @param height
* @return
*/
public Bitmap SetPhotoSize(Bitmap src,int weight,int height){
if(src!=null){
int w=src.getWidth();
int h=src.getHeight();
float scaleweight=((float)weight)/w;
float scaleheight=((float)height)/h;
Matrix matrix=new Matrix();
matrix.postScale(scaleweight, scaleheight);
//Bitmap newbitmap=null;
//newbitmap=Bitmap.createScaledBitmap(src, weight, height, true);
return Bitmap.createBitmap(src,0,0,w,h,matrix,true);
}
else{
Log.e("SetPhotoSize", "图片为空,无法修改大小!!");
return null;
}
}
/**
* 保存图片到文件夹的根目录
* @param photo
* @param photoname
*/
public void SavePhoto(Bitmap photo,String photoname){
File file=new File("/mnt/sdcard/DCIM/",photoname+".jpg");
FileOutputStream out=null;
try {
out=new FileOutputStream(file);
photo.compress(CompressFormat.JPEG, 100, out);
out.close();
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
/**
* 对图片进行旋转
* @param bitmap
* @return
*/
public Bitmap TurnPhoto(Bitmap bitmap){
Matrix matrix=new Matrix();
matrix.postRotate((float)90.0);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
}
/**
* 添加边框
* @param resources getresources();
* @param bm
* @param res
* @return
*/
public Bitmap addBigFrame(Resources resources,Bitmap bm,int res){
Bitmap bitmap=decodeBitmap(resources, res);
Drawable[] array=new Drawable[2];
array[0]=new BitmapDrawable(bm);
Bitmap b=SetPhotoSize(bitmap, bm.getWidth(), bm.getHeight());
array[1]=new BitmapDrawable(b);
LayerDrawable layer=new LayerDrawable(array);
return drawableToBitmap(layer);
}
/**
*添加水印
* @param src
* @param watermark
* @return
*/
public Bitmap addWaterMark(Bitmap src,Bitmap watermark){
if(src!=null){
int w=src.getWidth();
int h=src.getHeight();
int markw=watermark.getWidth();
int marky=watermark.getHeight();
Bitmap newwatermark=Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas cv=new Canvas(newwatermark);
//分别将两图片画到画布上
cv.drawBitmap(src,0,0,null);
cv.drawBitmap(watermark, 0, 0, null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newwatermark;
}
else{
Log.e("addWaterMark", "图片为空,无法添加水印!!");
return null;
}
}
/**
* 从drawable转到Bitmap方法
* @param drawable
* @return
*/
public Bitmap drawableToBitmap(Drawable drawable){
BitmapDrawable bitmap=(BitmapDrawable)drawable;
return bitmap.getBitmap();
}
/**
* 将资源文件的R.drawable.*转换为Bitmap
* @param resorrces
* @param res
* @return
*/
public Bitmap decodeBitmap(Resources resorrces,int res){
return BitmapFactory.decodeResource(resorrces, res);
}
/**
* 怀旧图片
* 公式 R=0.393*R+0.769*G+0.189*B;
* G=0.349*R+0.686*G+0.168*B;
* B=0.272*R+0.534*G+0.131*B;
* @param bitmap
* @return
*/
public Bitmap oldRember(Bitmap bitmap){
//long start=System.currentTimeMillis();
int width=bitmap.getWidth();
int height=bitmap.getHeight();
Bitmap bmp=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
int pixColor=0;
int pixR=0;
int pixG=0;
int pixB=0;
int newR=0;
int newG=0;
int newB=0;
int[] pixels=new int[width*height]; //获得所有的像素点
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for(int i=0;i<height;i++){
for(int k=0;k<width;k++){
pixColor=pixels[width*i+k];
pixR=Color.red(pixColor);
pixG=Color.green(pixColor);
pixB=Color.blue(pixColor);
newR=(int)(0.393*pixR+0.769*pixG+0.189*pixB);
newG=(int)(0.349*pixR+0.686*pixG+0.168*pixB);
newB=(int)(0.272*pixR+0.534*pixG+0.131*pixB);
int newColor=Color.argb(255, newR>255?255:newR,newG>255?255:newG , newB>255?255:newB);
pixels[width*i+k]=newColor;
}
}
bmp.setPixels(pixels, 0, width, 0, 0, width, height);
return bmp;
}
/**
* 光照效果
* @param bitmap
* strength调节光的强度
* @return
*/
public Bitmap Sunshine(Bitmap bitmap){
int width=bitmap.getWidth();
int height=bitmap.getHeight();
Bitmap bmp=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
int pixColor=0;
int pixR=0;
int pixG=0;
int pixB=0;
int newR=0;
int newG=0;
int newB=0;
int centerx=width/2;
int centery=height/2;
int radius=Math.min(centerx, centery);
final float strength=150f; //光照强度100--150
int[] pixels=new int[width*height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
int pos=0;
for(int i=0,length=height-1;i<length;i++){
for(int k=1,len=width-1;k<len;k++){
pos=i*width+k;
pixColor=pixels[pos];
pixR=Color.red(pixColor);
pixG=Color.green(pixColor);
pixB=Color.blue(pixColor);
newR=pixR;
newG=pixG;
newB=pixB;
int distance=(int) (Math.pow((centery-i),2)+Math.pow((centerx-k), 2));
if(distance<radius*radius){
int result=(int) (strength*(1.0-Math.sqrt(distance)/radius));
newR=pixR+result;
newB=pixB+result;
newG=pixG+result;
}
newR=Math.min(255, Math.max(0, newR));
newB=Math.min(255, Math.max(0, newB));
newG=Math.min(255, Math.max(0, newG));
pixels[pos]=Color.argb(255, newR, newG, newB);
}
}
bmp.setPixels(pixels, 0, width, 0, 0, width, height);
return bmp;
}
/**
* 圆角图片
* @param bitmap
* @param roundpx
* @return
*/
public Bitmap getRoundBitmap(Bitmap bitmap,float roundpx){
Bitmap out=Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas=new Canvas(out);
final int color=0xff424242;
final Paint paint=new Paint();
Rect rect =new Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
final RectF rectf=new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectf, roundpx, roundpx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); //先绘一个圆形,再绘一个Bitmap,交际即为圆角图片
canvas.drawBitmap(bitmap, rect,rect,paint);
return out;
}
public String getPhotoName(){
Date data=new Date(System.currentTimeMillis());
SimpleDateFormat dataFormat=new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
return dataFormat.format(data);
}
public Bitmap getCircleImage(Bitmap src ,int min){
final Paint paint=new Paint();
paint.setAntiAlias(true);
Bitmap target=Bitmap.createBitmap(min, min, Config.ARGB_8888);
Canvas canvas=new Canvas(target);
canvas.drawCircle(min/2, min/2, min/2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(src, 0, 0, paint);
return target;
}
}
0 0
- android的PhotoShop
- 用Photoshop打造Android 风格的Icon
- [photoshop] 基本的photoshop操作
- [Photoshop]Photoshop CS5的下载、安装、破解
- Photoshop 的历史
- Photoshop 的历史
- PHOTOSHOP快捷键[隐藏的]
- photoshop的操作密集
- PHOTOSHOP的技巧
- photoshop的色彩模式
- PhotoShop的六大功能
- 去广工考ITAT的photoshop
- photoshop的一点笔记
- 木匠师傅的 Photoshop
- PhotoShop效果的数学公式
- photoshop的一些技巧
- 关于PhotoShop的常识
- Photoshop的智能对象
- ANDROID 中设计模式的采用--创建型模式
- Spring AMQP消息监听容器配置
- STL list
- 举例说明 public private protected 的区别
- N-gram统计语言模型(总结)
- android的PhotoShop
- 【转】c# winform TreeView控件中实现拖拽的功能
- MVC 检测用户是否已经登录
- ECShop出现Strict Standards: Only variables should be passed by reference in的解决方法
- vijos P1121 马拦过河卒
- 第十二周项目四(1)童氏银行框架版
- 三角形的判断
- java WebSocket 简易聊天消息推送
- 回车符(CR)与换行符(LF)