安卓开发中Bitmap源码实例

 安卓开发中Bitmap源码实例

  package android.graphics;

  import java.awt.image.BufferedImage;

  import java.io.File;

  import java.io.IOException;

  import java.io.InputStream;

  import javax.imageio.ImageIO;

  public final class Bitmap extends _Original_Bitmap {

  private BufferedImage mImage;

  public Bitmap(File input) throws IOException {

  super(1, true, null, -1);

  mImage = ImageIO.read(input);

  }

  public Bitmap(InputStream is) throws IOException {

  super(1, true, null, -1);

  mImage = ImageIO.read(is);

  }

  Bitmap(BufferedImage image) {

  super(1, true, null, -1);

  mImage = image;

  }

  public BufferedImage getImage() {

  return mImage;

  }

  // ----- overriden methods

  public enum Config {

  // these native values must match up with the enum in SkBitmap.h

  ALPHA_8 (2),

  RGB_565 (4),

  ARGB_4444 (5),

  ARGB_8888 (6);

  Config(int ni) {

  this.nativeInt = ni;

  }

  final int nativeInt;

  /* package */ static Config nativeToConfig(int ni) {

  return sConfigs[ni];

  }

  private static Config sConfigs[] = {

  null, null, ALPHA_8, null, RGB_565, ARGB_4444, ARGB_8888

  };

  }

  @Override

  public int getWidth() {

  return mImage.getWidth();

  }

  @Override

  public int getHeight() {

  return mImage.getHeight();

  }

  /**

  * Returns an immutable bitmap from the source bitmap. The new bitmap may

  * be the same object as source, or a copy may have been made.

  */

  public static Bitmap createBitmap(Bitmap src) {

  return createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), null, false);

  }

  /**

  * Returns an immutable bitmap from the specified subset of the source

  * bitmap. The new bitmap may be the same object as source, or a copy may

  * have been made.

  *

  * @param source The bitmap we are subsetting

  * @param x The x coordinate of the first pixel in source

  * @param y The y coordinate of the first pixel in source

  * @param width The number of pixels in each row

  * @param height The number of rows

  */

  public static Bitmap createBitmap(Bitmap source, int x, int y,

  int width, int height) {

  return new Bitmap(source.mImage.getSubimage(x, y, width, height));

  }

  /**

  * Returns an immutable bitmap from subset of the source bitmap,

  * transformed by the optional matrix.

  *

  * @param source The bitmap we are subsetting

  * @param x The x coordinate of the first pixel in source

  * @param y The y coordinate of the first pixel in source

  * @param width The number of pixels in each row

  * @param height The number of rows

  * @param m Option matrix to be applied to the pixels

  * @param filter true if the source should be filtered.

  * Only applies if the matrix contains more than just

  * translation.

  * @return A bitmap that represents the specified subset of source

  * @throws IllegalArgumentException if the x, y, width, height values are

  * outside of the dimensions of the source bitmap.

  */

  public static Bitmap createBitmap(Bitmap source, int x, int y, int width,

  int height, Matrix m, boolean filter) {

  checkXYSign(x, y);

  checkWidthHeight(width, height);

  if (x + width > source.getWidth()) {

  throw new IllegalArgumentException(

  "x + width must be <= bitmap.width()");

  }

  if (y + height > source.getHeight()) {

  throw new IllegalArgumentException(

  "y + height must be <= bitmap.height()");

  }

  // check if we can just return our argument unchanged

  if (!source.isMutable() && x == 0 && y == 0

  && width == source.getWidth() && height == source.getHeight()

  && (m == null || m.isIdentity())) {

  return source;

  }

  if (m == null || m.isIdentity()) {

  return new Bitmap(source.mImage.getSubimage(x, y, width, height));

  }

  int neww = width;

  int newh = height;

  Paint paint;

  Rect srcR = new Rect(x, y, x + width, y + height);

  RectF dstR = new RectF(0, 0, width, height);

  /* the dst should have alpha if the src does, or if our matrix

  doesn't preserve rectness

  */

  boolean hasAlpha = source.hasAlpha() || !m.rectStaysRect();

  RectF deviceR = new RectF();

  m.mapRect(deviceR, dstR);

  neww = Math.round(deviceR.width());

  newh = Math.round(deviceR.height());

  Canvas canvas = new Canvas(neww, newh);

  canvas.translate(-deviceR.left, -deviceR.top);

  canvas.concat(m);

  paint = new Paint();

  paint.setFilterBitmap(filter);

  if (!m.rectStaysRect()) {

  paint.setAntiAlias(true);

  }

  canvas.drawBitmap(source, srcR, dstR, paint);

  return new Bitmap(canvas.getImage());

  }

  /**

  * Returns a mutable bitmap with the specified width and height.

  *

  * @param width The width of the bitmap

  * @param height The height of the bitmap

  * @param config The bitmap config to create.

  * @throws IllegalArgumentException if the width or height are <= 0

  */

  public static Bitmap createBitmap(int width, int height, Config config) {

  return new Bitmap(new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB));

  }

  /**

  * Returns a immutable bitmap with the specified width and height, with each

  * pixel value set to the corresponding value in the colors array.

  *

  * @param colors Array of {@link Color} used to initialize the pixels.

  * @param offset Number of values to skip before the first color in the

  * array of colors.

  * @param stride Number of colors in the array between rows (must be >=

  * width or <= -width).

  * @param width The width of the bitmap

  * @param height The height of the bitmap

  * @param config The bitmap config to create. If the config does not

  * support per-pixel alpha (e.g. RGB_565), then the alpha

  * bytes in the colors[] will be ignored (assumed to be FF)

  * @throws IllegalArgumentException if the width or height are <= 0, or if

  * the color array's length is less than the number of pixels.

  */

  public static Bitmap createBitmap(int colors[], int offset, int stride,

  int width, int height, Config config) {

  checkWidthHeight(width, height);

  if (Math.abs(stride) < width) {

  throw new IllegalArgumentException("abs(stride) must be >= width");

  }

  int lastScanline = offset + (height - 1) * stride;

  int length = colors.length;

  if (offset < 0 || (offset + width > length)

  || lastScanline < 0

  || (lastScanline + width > length)) {

  throw new ArrayIndexOutOfBoundsException();

  }

  // TODO: create an immutable bitmap...

  throw new UnsupportedOperationException();

  }

  /**

  * Returns a immutable bitmap with the specified width and height, with each

  * pixel value set to the corresponding value in the colors array.

  *

  * @param colors Array of {@link Color} used to initialize the pixels.

  * This array must be at least as large as width * height.

  * @param width The width of the bitmap

  * @param height The height of the bitmap

  * @param config The bitmap config to create. If the config does not

  * support per-pixel alpha (e.g. RGB_565), then the alpha

  * bytes in the colors[] will be ignored (assumed to be FF)

  * @throws IllegalArgumentException if the width or height are <= 0, or if

  * the color array's length is less than the number of pixels.

  */

  public static Bitmap createBitmap(int colors[], int width, int height,

  Config config) {

  return createBitmap(colors, 0, width, width, height, config);

  }

  public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,

  int dstHeight, boolean filter) {

  Matrix m;

  synchronized (Bitmap.class) {

  // small pool of just 1 matrix

  m = sScaleMatrix;

  sScaleMatrix = null;

  }

  if (m == null) {

  m = new Matrix();

  }

  final int width = src.getWidth();

  final int height = src.getHeight();

  final float sx = dstWidth / (float)width;

  final float sy = dstHeight / (float)height;

  m.setScale(sx, sy);

  Bitmap b = Bitmap.createBitmap(src, 0, 0, width, height, m, filter);

  synchronized (Bitmap.class) {

  // do we need to check for null? why not just assign everytime?

  if (sScaleMatrix == null) {

  sScaleMatrix = m;

  }

  }

  return b;

  }

  }

 
 

时间: 2024-08-31 09:21:56

安卓开发中Bitmap源码实例的相关文章

二维码扫描-安卓开发中的Device device = new Device();是什么意思啊!

问题描述 安卓开发中的Device device = new Device();是什么意思啊! Device device = new Device(); device.setKeyNum(""); device.setcodingNum(scanResult); 这几句代码什么意思啊 百度不出来怎么 安卓项目里 的 解决方案 Device device = new Device(); //创建一个Device对象实例 device.setKeyNum("");//

android开发-如何用Android Studio将源码打包成AAR文件,并且AAR文件中包含源码

问题描述 如何用Android Studio将源码打包成AAR文件,并且AAR文件中包含源码 如何用Android Studio将源码打包成AAR文件,并且AAR文件中包含源码.类似与jar文件打包后可以看到源代码一样. 解决方案 android studio本地引用aar打包文件(.aar)Android studio 打包aarandroid studio 把源码和资源文件一起打包生成aar包

android-java语言 安卓开发中的问题

问题描述 java语言 安卓开发中的问题 Pattern ptn=Pattern.compile("[(d{2}:d{2}.d{2})]"): Matcher mtn=ptn.matcher(line): 这两句是什么意思? 解决方案 正则表达式,匹配 [11:22.33]这样的字符串,d{2}表示2位数字. 解决方案二: Java语言是一种支持多线程的语言,它通过同步(互斥)和协作(等待和唤醒)来完成.这里聊聊同步. ? 线程不安全主要来自于类变量(静态变量)和实例变量,前者位于方法

eclipse中的源码如何打包为.exe文件

问题描述 eclipse中的源码如何打包为.exe文件 如何将eclipse中编写的程序源码导出,最后转为.exe文件,就像网上下载的软件那样的格式,让那些没安装eclipse的电脑也能运行你写的程序,求赐教.详细的步骤和使用的软件 解决方案 eclipse只是开发工具,它支持很多平台和编程语言,假设你用java编写客户端程序,那么可以参考http://jingyan.baidu.com/article/358570f6727bb7ce4724fc88.htmlhttp://blog.csdn.

android开发-安卓开发中的文件操作都有哪些?

问题描述 安卓开发中的文件操作都有哪些? 现在需要做的是一个文件和图片相关的软件,想要知道有哪些文件操作相关命令概括的比较全的文档或是代码实例,自己找的都有点偏 解决方案 http://www.cnblogs.com/devinzhang/archive/2012/01/19/2327597.html 解决方案二: iOS开发 文件操作android 开发中的文件操作android开发中的文件操作

安卓应用开发-安卓开发中红米手机拍照发出去后为啥旋转了90度

问题描述 安卓开发中红米手机拍照发出去后为啥旋转了90度 10C 开发过程中,做了一个类似微信朋友圈的东西,拍照上传图片的时候,在红米手机上遇到了这个问题,本来是正着拍摄,发出去后在朋友圈显示的是旋转了90度的,如图所示,键盘旋转了90度 解决方案 跟手机有关,手机拍照默认横屏 解决方案二: 我也是红米,做移动开发时拍照是正常的

安卓开发中某一个界面还没等你操作呢 自己就没了 哪里出问题了?

问题描述 安卓开发中某一个界面还没等你操作呢 自己就没了 哪里出问题了? 问题如上 好困惑! 解决方案 自己就没了,是返回上一级页面么?还是什么?有可能是AndroidManifest.xml文件里activity配置的问题.. 解决方案二: 出现fatal crash了把,抓下log看看. 解决方案三: 贴出代码,贴出log,才好分析,这样描述太抽象 解决方案四: 看一下log日志,看看错误信息. 解决方案五: 这个问题问的有点抽象能不能再具体些

安卓开发中不能建立wifi热点,可以连接热点,附带错误日志,希望大神指教。

问题描述 安卓开发中不能建立wifi热点,可以连接热点,附带错误日志,希望大神指教.

全显示成正数-安卓开发中如何使一个byte类型的数组内的数据不显示成负数

问题描述 安卓开发中如何使一个byte类型的数组内的数据不显示成负数 在做Android开发的时候一个byte类型的数组result,里面有比较大的正数,需要做什么样的处理才能使他显示的全是正数:求大神指导,给个代码 byte[] result for (int i = 0; i < result.length; i++) Log.e("读出全部page", "byte " + i + " is " + result[i]); 解决方案 f