android 如何使图片变灰 android 如何使图片变灰 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以看看android.graphics.Bitmap类,里面应该有方法处理。 是把彩色图像转换为灰度图是不是?我帮你写了一段代码,测试通过。方法的两个参数分别是源文件,目标文件在Android内的路径位置。要注意的是,存取SD卡的话,必须在AndroidManifest.xml文件中加上一行<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />否则创建文件会失败。package com.example;import java.io.*;import android.graphics.*;import android.graphics.Bitmap.CompressFormat;public class ReadImage{ public static void toGrayImage(String source, String dest) { try { Bitmap bitmap = BitmapFactory.decodeFile(source); int width = bitmap.getWidth(); int height = bitmap.getHeight(); Bitmap grayImg = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // Canvas canvas = new Canvas(grayImg); Paint paint = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter( colorMatrix); paint.setColorFilter(colorMatrixFilter); canvas.drawBitmap(bitmap, 0, 0, paint); // canvas. File file = new File(dest); boolean success = file.createNewFile(); FileOutputStream stream = new FileOutputStream(file); grayImg.compress(CompressFormat.JPEG, 100, stream); stream.flush(); stream.close(); bitmap.recycle(); grayImg.recycle(); } catch (Exception e) { @SuppressWarnings("unused") String msg = e.getMessage(); } }} 关于一个Activity的destroy问题,请大神帮忙下!!! android像素问题 视频播放中按Home Key后,再进入视频播放时的问题!!!(在线等) 关于enum问题 android设备在局域网内能否实现短信和通话,不用手机卡 Remoteviews上的Imageview如何拖动,急!急!急! 请教一个关于真机与模拟器发送httpget请求返回不相同的问题? ListView显示数据的问题 关于ucweb中的menu实现的问题 求助 很抱歉"****"已停止运行 Eclipse下java编程, 能不能实现可视化编程? 圖標拖動的問題
我帮你写了一段代码,测试通过。方法的两个参数分别是源文件,目标文件在Android内的路径位置。
要注意的是,存取SD卡的话,必须在AndroidManifest.xml文件中加上一行
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />
否则创建文件会失败。
package com.example;import java.io.*;
import android.graphics.*;
import android.graphics.Bitmap.CompressFormat;public class ReadImage
{
public static void toGrayImage(String source, String dest)
{
try
{
Bitmap bitmap = BitmapFactory.decodeFile(source);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap grayImg = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//
Canvas canvas = new Canvas(grayImg); Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(
colorMatrix);
paint.setColorFilter(colorMatrixFilter);
canvas.drawBitmap(bitmap, 0, 0, paint);
// canvas.
File file = new File(dest);
boolean success = file.createNewFile();
FileOutputStream stream = new FileOutputStream(file);
grayImg.compress(CompressFormat.JPEG, 100, stream);
stream.flush();
stream.close();
bitmap.recycle();
grayImg.recycle();
}
catch (Exception e)
{
@SuppressWarnings("unused")
String msg = e.getMessage();
} }
}