这是我的尝试(丑陋的GDI + 和 GDI混合......)
// ...
BYTE pixels[BMP_WIDTH * BMP_HEIGHT * BMP_BPP];
HBITMAP hBitmap;
Gdiplus::Bitmap cBitmap(BMP_WIDTH, BMP_HEIGHT, PixelFormat32bppRGB);
Gdiplus::Graphics cGraphics(&cBitmap);
Gdiplus::Pen cPen(Gdiplus::Color(255, 255, 0, 0));
cGraphics.DrawRectangle(&cPen, 0, 0, cBitmap.GetWidth() - 1, cBitmap.GetHeight() - 1);
// and here it get's real ugly, I'd like to change that...
cBitmap.GetHBITMAP(Gdiplus::Color(255, 255, 255), &hBitmap);
GetBitmapBits(hBitmap, sizeof(pixels), pixels);
// ...
Run Code Online (Sandbox Code Playgroud)
有人告诉我使用LockBits,但我真的不明白怎么做.我试过了,但是我失败了,所以我也不打算发布那个尝试.
在处理Android中的一些位图时,我注意到视图中使用的白色并不总是与位图上呈现的白色相同.考虑这个截图.

背景白色是从与白色背景颜色的一个看法.
前景"白色"来自从SD卡解码的白色位图,显示在ImageView中.使用RGB_565以下方法解码此位图:
BitmapFactory.Options resample = new BitmapFactory.Options();
resample.inPreferredConfig = Config.RGB_565;
resample.inSampleSize = sampleSize;
return BitmapFactory.decodeFile(filePath, resample);
Run Code Online (Sandbox Code Playgroud)
作为参考,这是位图.
为什么这个以及如何解决?
我正在尝试将一个微小的图像(像20x40像素)缩放到任意大的图像.我通过设置ImageView.scaleType财产做缩放,但同时它缩放图像,它从一个原始像素的颜色下一变淡,创建图像的模糊的大版本.
这是问题的一些背景.我正在尝试为Android创建一个老式的16位风格游戏.我想将sprite/etc存储为小文件,然后根据用户的屏幕大小将它们放大.这样,我不必为每个精灵创建大量不同大小的不同图像.现在我正在研究一个概念验证,所以我试图将一个小的20x40图像文件缩放到整个屏幕的大小.
*编辑:我想出了如何使用Reflog推荐的方法.但是,它仍然会有一些衰落,远远低于默认算法.
*edit2:搞定了!我不得不关闭抗锯齿
*edit3:这神秘地停止了我的工作,我找到了另一个需要禁用抖动/自动缩放的地方.添加了选项行.
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setDither(false);
paint.setAntiAlias(false);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tinyimg, options);
canvas.drawBitmap(bitmap, null, new RectF(getLeft(), getTop(), getRight()/2, getBottom()/2), paint);
}
Run Code Online (Sandbox Code Playgroud) 根据主题"将TextView截屏到位图"中找到了很多帖子.
嗯,我的问题的不同之处在于,首先在显示器上绘制视图(已完成所有布局和测量工作),然后绘制到连接到位图的Canvas.
我只是想从头开始创建一个TextView,而不是在显示器上显示,它被渲染成一个Bitmap.
这个是已经工作的基础配置.单击TextView将自身绘制到Bitmap中并将其设置为ImageView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="#fff">
<TextView android:id="@+id/tv" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="The Quick Brown Fox Jumps Over The Lazy Dog."
android:textSize="20dip" android:background="#abcdef"
android:textColor="#000" android:padding="10dip"
android:layout_margin="10dip" />
<ImageView android:id="@+id/iv" android:layout_width="449px"
android:layout_height="47px" android:background="#56789a"
android:layout_margin="10dip" />
</LinearLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.tv).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bmp = Bitmap.createBitmap(449, 47, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
v.draw(canvas);
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bmp);
}
});
}
Run Code Online (Sandbox Code Playgroud)
现在是问题部分.我将在Java中创建一个TextView,我希望将其直接绘制到Bitmap中.在此之后,我将其设置为ImageView.我从没跑过这个:(
Bitmap …Run Code Online (Sandbox Code Playgroud) 我是6个月前开始Android开发的"初学者".我对这个话题非常热情,我读过几本书:
这些书使我明白了很多 Android的工作方式.但是现在缺少的是......经验.所以我决定为一个项目做出贡献,该项目的目标是显示一个巨大的地铁地图 - 当然,项目的目标更广泛,但谈论它与这个问题无关.
地图在我的应用程序包中离线存储.
此外,仅使用一个大图像无法做到这一点:它会导致极端的图像质量损失.
我将我的巨大地图分成5个不同的地图图块,这些图块基本上是横向格式的矩形.我把它们全部(map [1-5] .jpg)和一个HTML文件(map.html)放到./assets/文件夹中.HTML文件的代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Huge map</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" …Run Code Online (Sandbox Code Playgroud) 我在扩展表面视图和实现runnable的类中有这个代码我能够使用该类基本上允许您使用不同的颜色等绘制到画布.我正在尝试获得一种方法,允许我在绘制图像后保存图像,这是方法.无论我做什么,我只是得到一个没有任何内容的黑色图像.有任何想法吗?
我启用了缓存绘图
目标从自定义SurfaceView获取位图图像我已经用尽了在这里查看其他帖子的选项,但没有找到任何可行的工作.这里希望最近有一个新的解决方案.非常感谢
public Bitmap getImage() {
Bitmap bitmap = Bitmap.createBitmap(this.getWidth(),
this.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
this.draw(canvas);
return bitmap;
}
Run Code Online (Sandbox Code Playgroud) 我试图从视频中提取所有帧.
通过以下代码我想获取视频的前30帧,但我只得到第一帧30次.
private ArrayList<Bitmap> getFrames(String path) {
try {
ArrayList<Bitmap> bArray = new ArrayList<Bitmap>();
bArray.clear();
MediaMetadataRetriever mRetriever = new MediaMetadataRetriever();
mRetriever.setDataSource("/sdcard/myvideo.mp4");
for (int i = 0; i < 30; i++) {
bArray.add(mRetriever.getFrameAtTime(1000*i,
MediaMetadataRetriever.OPTION_CLOSEST_SYNC));
}
return bArray;
} catch (Exception e) { return null; }
}
Run Code Online (Sandbox Code Playgroud)
现在,我如何从视频中获取所有帧?
试图实现图像的徒手裁剪,所以我能够在图像上绘图.但它超出了位图区域.我只想限制用户只能在位图区域内绘制,检查下面的屏幕截图.
我正在尝试实现像Photoshop套索工具这样的功能.
它在视图区域外绘制,产生不正确的输出.

产量

码@
的onDraw
public void onDraw(Canvas canvas) {
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawBitmap(bitmap, rect, rect, null);
// RectF r = new RectF();
// Matrix matrix = new Matrix();
// matrix.mapRect(r);
// Log.i(TAG, "Rect " + r.left + " " + r.top + " " + r.right + " " +
// r.bottom + " ");
// canvas.clipRect(r.left, r.top, r.right, r.bottom);
Path path = new Path();
boolean first = true;
for …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用新的Android脸部检测移动视觉API来处理帧图像.
所以我创建了自定义检测器以获取Frame并尝试调用getBitmap()方法,但它为null,因此我访问了帧的灰度数据.有没有办法从它创建位图或类似的图像持有者类?
public class CustomFaceDetector extends Detector<Face> {
private Detector<Face> mDelegate;
public CustomFaceDetector(Detector<Face> delegate) {
mDelegate = delegate;
}
public SparseArray<Face> detect(Frame frame) {
ByteBuffer byteBuffer = frame.getGrayscaleImageData();
byte[] bytes = byteBuffer.array();
int w = frame.getMetadata().getWidth();
int h = frame.getMetadata().getHeight();
// Byte array to Bitmap here
return mDelegate.detect(frame);
}
public boolean isOperational() {
return mDelegate.isOperational();
}
public boolean setFocus(int id) {
return mDelegate.setFocus(id);
}}
Run Code Online (Sandbox Code Playgroud) 我在Google Developer Console中收到崩溃报告并显示中止消息:"错误,无法在此处访问无效/免费的位图!".下面的堆栈跟踪是迄今为止我能够获得的所有内容.该问题仅发生在Android 6.0设备(marshmallow)上.我无法在任何设备上重现该问题.看到这个的用户甚至无法打开应用程序; 它甚至在呈现应用程序的任何可视化表示之前崩溃.由于错误涉及位图,我经历了整个项目寻找损坏的图像文件,但我没有找到任何.有什么建议?谢谢!
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint:'google/ryu/dragon:6.0.1/MXC89L/3084571:user/release-keys'
Revision: '0'
ABI: 'arm64'
pid: 15550, tid: 15550, name: appname >>> com.domain.appname <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Abort message: 'Error, cannot access an invalid/free'd bitmap here!'
x0 0000000000000000 x1 0000000000003cbe x2 0000000000000006 x3 0000000000000000
x4 0000000000000000 x5 0000000000000001 x6 0000000000000000 x7 0000000000000000
x8 0000000000000083 x9 000000000000003c x10 0000007fcd9a8e2c x11 0000007fcd9a8e68
x12 0000007fcd9a8d40 …Run Code Online (Sandbox Code Playgroud)