我需要将位图转换为我的硬件的二进制图像。你知道吗?
我想创建特定图像的位图图像,但我想抵抗位图图像的宽度和高度。我正在使用以下代码......
image2 = BitmapFactory.decodeResource(getResources(), rid);
Log.w("DEBUG","which is null:image i " + i.getWidth() + " OR " +i.getDrawable().getIntrinsicHeight () );
Log.w("DEBUG","which is null:image h " + h.getWidth() + " OR " +h.getHeight() );
Log.w("DEBUG","which is null:image2 " + image2.getWidth() + " OR " +image2.getHeight() );
Bitmap image = Bitmap.createScaledBitmap(image2, i.getWidth(), i.getHeight(), false);
Log.w("DEBUG","which is null:image " + image.getWidth() + " OR " +image.getHeight() );
double bmWidth = image.getWidth();
double bmHeight = image.getHeight();
if ( x < 0 || y < 0 …Run Code Online (Sandbox Code Playgroud) 在发布之前,我真的在整个网络上搜索过.我的问题是我无法在不损失图像质量的情况下调整位图大小(质量非常差并且像素化).
我从相机获取位图然后我必须缩小它,所以我可以更快地将它上传到服务器.
这是进行采样的功能
public Bitmap resizeBitmap(Bitmap bitmap){
Canvas canvas = new Canvas();
Bitmap resizedBitmap = null;
if (bitmap !=null) {
int h = bitmap.getHeight();
int w = bitmap.getWidth();
int newWidth=0;
int newHeight=0;
if(h>w){
newWidth = 600;
newHeight = 800;
}
if(w>h){
newWidth = 800;
newHeight = 600;
}
float scaleWidth = ((float) newWidth) / w;
float scaleHeight = ((float) newHeight) / h;
Matrix matrix = new Matrix();
// resize the bit map
matrix.preScale(scaleWidth, scaleHeight);
resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数,可以为我正在制作的游戏快速绘制菜单图像。我可以很好地绘制背景和文本块,但在屏幕上创建位图图像时遇到问题
bool menu::drawMenu(PAINTSTRUCT ps)
{
HWND hWnd = GetActiveWindow();
HDC hdc = GetDC(hWnd), hdcMem;
//Draw a new background
HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
HBRUSH blackBrush = CreateSolidBrush(RGB(0, 0, 0));
SelectObject(hdc, blackPen);
SelectObject(hdc, blackBrush);
Rectangle(hdc, 0, 0, 1080, 720);
//insert selection text
TextOut(hdc, 30, 0, L"New Game", 8);
TextOut(hdc, 30, 30, L"Exit Game", 9);
//draw arrow sprite
HBITMAP arrow = (HBITMAP)LoadImage(NULL, L"C:\\Users\\Tim\documents\\visual studio 2013\\Projects\\BoulderBisque\\BoulderBisque\\arrow.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
DWORD lastError = GetLastError();
if (arrow == NULL)
{
TextOut(hdc, …Run Code Online (Sandbox Code Playgroud) 我正在创建一个叠加图像,它将作为一种过滤器位于另一个图像之上,强调我希望用户关注的图像部分.基本上,图像在某些地方只是纯白色,而在其他地方则是透明的,在这些部分之间模糊.
我正在尝试将此图像保存在我的数据库中,但无法使压缩工作.下面是我遇到问题的代码片段.
Bitmap image = Bitmap.createBitmap(width, height, Config.ALPHA_8)
... // create image
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream); // always returns false
Run Code Online (Sandbox Code Playgroud)
我已经尝试将这些图像保存到文件中,但结果相同.交替压缩格式,质量(在JPEGS的情况下)也没有区别.我可以使用的唯一alpha启用格式是ARGB_8888.这对我来说是一个问题,因为我的图像高达1000px x 1000px,所以我需要节省空间.每像素分配4个字节会导致内存问题.
在Android上压缩ALPHA_8图像是一个已知问题吗?是否需要设置一组特定的位图选项才能使其正常工作?我环顾了各种各样的互联网,虽然有些人似乎也经历过这种情况,但大多数人似乎只是转向ARGB_8888选项并没有ALPHA_8的解决方案
任何帮助表示赞赏
我想创建一个带阴影的文本位图,但我无法得到好的结果。问题是,当我直接绘制文本时,它看起来不错,但是当我将文本绘制到位图,然后绘制位图时,它看起来很丑陋。
代码:
public class MyView extends View {
private Paint paint;
private Bitmap bitmap;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void init(){
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(50);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.create("HELVETICA", Typeface.NORMAL));
paint.setShadowLayer(30, 0, 0, Color.BLACK);
bitmap = Bitmap.createBitmap(500, 300, Bitmap.Config.ARGB_8888);
Canvas canvas2 = new Canvas(bitmap);
canvas2.drawText("Dec Use", 100, 100, paint);
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
final boolean useBitmap = true;
if(useBitmap){
canvas.drawBitmap(bitmap, 0, 0, null);
}
else{
canvas.drawText("Dec Use", 100, 100, paint);
} …Run Code Online (Sandbox Code Playgroud) 初始代码是官方文档:Loading Large Bitmaps Efficiently。
我开始四处寻找,发现图像没有按照文档中的描述调整大小:
如果设置为 > 1 的值,则请求解码器对原始图像进行二次采样,返回较小的图像以节省内存。样本大小是对应于解码位图中单个像素的任一维度中的像素数。例如,inSampleSize == 4 返回一个图像,其宽度/高度是原始图像的 1/4,像素数是 1/16。任何 <= 1 的值都被视为与 1 相同。 注意:解码器使用基于 2 的幂的最终值,任何其他值将向下舍入到最接近的 2 的幂。
当我运行代码时:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
Log.e(LOG_TAG, "orig img size " + options.outWidth + "x" +
options.outHeight);
// Calculate inSampleSize
// options.inJustDecodeBounds = false; // version 2
for (int i = 2; i<20; i*=2) {
options.inSampleSize = i;
Log.d(LOG_TAG, "inSampleSize: " + options.inSampleSize);
Bitmap b = …Run Code Online (Sandbox Code Playgroud) 我在使用 Android Studio 的“查看位图”预览功能时遇到问题。我有一个 29000 和高度 255 的位图。但是当我在位图类的调试器中单击“查看位图”时,我看到一个缩小的版本。是的,我知道 29k 宽度很多,但它与 Android Studio < 1.5 一起使用,所以我猜它是某个地方的设置。
这是调试器视图中有关位图的信息
当我单击“查看位图”时,这是位图。
如您所见,尺寸不正确。该调试预览位图比我想看到的要小得多。
任何建议如何解决这个问题?
我的项目是
\n\n我对 1) 和 3) 没有问题,但我可以 \xe2\x80\x99t 找到一种将文本添加到位图图形上下文的方法。我花了很多时间阅读一些帖子,但没有成功。\n文档(https://developer.apple.com/reference/coregraphics/cgcontext/1586505-showtextatpoint)说我应该考虑核心文本,但我\n不知道如何应该使用 Core Text 将一些文本添加到我的\n位图上下文中。我还读到 NSString 的绘制实例方法允许\n在当前图形上下文中写入文本,但据我所知当前\n图形上下文与我的位图图形上下文无关?
\n\n需要明确的是,我对在屏幕上绘制文本不感兴趣,我感兴趣的是在位图中绘制文本并将该位图保存在文件中。
\n\nlet colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()\n// Create the bitmap graphic context\nvar gc = CGContext(data: nil, width: 400, height: 400, bitsPerComponent: 8, bytesPerRow: 0,\n space: colorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)\n\n// draw an image (croppedCGImage is a CGImage instance)\nvar rect = CGRect(x: 0, y:0, width:200, height: 200)\ngc?.draw(croppedCGImage, in: rect)\n\n// draw some text ????\n// how to do that …Run Code Online (Sandbox Code Playgroud)