Java Code Examples for android.graphics.BitmapFactory.decodeByteArray()

The following are Jave code examples for showing how to use decodeByteArray() of the android.graphics.BitmapFactory class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Graphrec   File: CameraActivity.java   Source Code and License Vote up 7 votes
private byte[] proccessData(byte[] data) {

        SurfaceOverlay overlay = (SurfaceOverlay) findViewById(R.id.overlay);
        int width = overlay.getWidth()/2;
        int indicatorOffset = 200;

        int leftCornerX = width-indicatorOffset;
        int leftCornerY = indicatorOffset;
        int indicatorHeight = width;
        int indicatorWidth = width;

        Bitmap bmp= BitmapFactory.decodeByteArray(data,0,data.length);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Bitmap dst = Bitmap.createBitmap(bmp, leftCornerX, leftCornerY, indicatorWidth, indicatorHeight);
        dst = Bitmap.createScaledBitmap(dst, 28, 28, false);
        dst.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();

    }
 
Example 2
Project: androidtools   File: DiskFileCacheHelper.java   Source Code and License Vote up 6 votes
/**
 * byte[] → Bitmap
 */
private static Bitmap Bytes2Bimap(byte[] b) {
    if (b.length == 0) {
        return null;
    }
    return BitmapFactory.decodeByteArray(b, 0, b.length);
}
 
Example 3
Project: SIIEScanner   File: Header.java   Source Code and License Vote up 6 votes
public Bitmap createBitMap() {
    if( this.imageBitmap == null ){
        if( this.imageData == null ) return null;
        byte[] data = Base64.decode( this.imageData, Base64.DEFAULT);
        this.imageBitmap = BitmapFactory.decodeByteArray( data, 0, data.length);
    }
    return this.imageBitmap;
}
 
Example 4
Project: SudoCAM-Ku   File: CameraView.java   Source Code and License Vote up 6 votes
@Override
public void onPictureTaken(byte[] data, Camera camera) {
    Log.i(TAG, "Saving a bitmap to file");
    // The camera preview was automatically stopped. Start it again.
    mCamera.startPreview();
    mCamera.setPreviewCallback(this);

    // Write the image in a file (in jpeg format)
    try {
        /*FileOutputStream fos = new FileOutputStream(mPictureFileName);

        fos.write(data);
        fos.close();*/

        Bitmap bmp = BitmapFactory.decodeByteArray(data , 0, data.length);
        Mat orig = new Mat(bmp.getHeight(),bmp.getWidth(),CvType.CV_8UC3);
        Bitmap myBitmap32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
        Utils.bitmapToMat(myBitmap32, orig);
        mImage = new Mat();
        Imgproc.cvtColor(orig,mImage,Imgproc.COLOR_RGB2GRAY);
        /*Imgproc.cvtColor(orig, orig, Imgproc.COLOR_BGR2RGB,4);
        Mat frame = new Mat(mFrameHeight+mFrameHeight/2,mFrameWidth, CvType.CV_8UC1);
        frame.put(0,0,data);
        //Imgcodecs.imdecode(frame,0);
        Imgproc.cvtColor(frame,mImage,Imgproc.COLOR_YUV2RGBA_NV21);//frame.submat(0, mFrameHeight, 0, mFrameWidth);*/

    } catch (Exception e) {
        Log.e("PictureDemo", "Exception in photoCallback", e);
    }

}
 
Example 5
Project: rongyunDemo   File: BitmapUtils.java   Source Code and License Vote up 5 votes
/**
 * @param "将字节数组转换为ImageView可调用的Bitmap对象"
 * @param bytes
 * @param opts
 * @return Bitmap
 */
public static Bitmap getPicFromBytes(byte[] bytes,
                                     BitmapFactory.Options opts) {
    if (bytes != null)
        if (opts != null)
            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
                                                 opts);
        else
            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    return null;
}
 
Example 6
Project: tiny   File: FileCompressTestActivity.java   Source Code and License Vote up 5 votes
private void testBytes() {
    try {
        final InputStream is = getResources().getAssets()
                .open("test-3.jpg");

        long fileSize = is.available();

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int len = -1;
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        os.close();
        is.close();
        byte[] bitmapBytes = os.toByteArray();

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = mConfig;
        Bitmap originBitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length, options);

        setupOriginInfo(originBitmap, fileSize);

        Tiny.FileCompressOptions compressOptions = new Tiny.FileCompressOptions();
        compressOptions.config = mConfig;
        Tiny.getInstance().source(bitmapBytes).asFile().withOptions(compressOptions).compress(new FileCallback() {
            @Override
            public void callback(boolean isSuccess, String outfile) {
                if (!isSuccess) {
                    mCompressTv.setText("compress file failed!");
                    return;
                }
                File file = new File(outfile);
                setupCompressInfo(outfile, file.length());
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Project: Tess-TwoDemo   File: CameraActivity.java   Source Code and License Vote up 5 votes
@Override
public void onPictureTaken(CameraView cameraView, final byte[] data) {
    Log.d(TAG, "onPictureTaken " + data.length);
    Bitmap bitmap = null;
    if (data != null) {
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//data是字节数据,将其解析成类图
    }
    //保存图片到sdcard
    if (bitmap != null) {
        if (rectPictureSize == null) {
            rectPictureSize = DisplayUtils.createCenterPictureRect(ratio, cameraRatio, bitmap.getWidth(), bitmap.getHeight());
        }
        int x = bitmap.getWidth() / 2 - rectPictureSize.x / 2;
        int y = bitmap.getHeight() / 2 - rectPictureSize.y / 2;
        Bitmap rectBitmap = Bitmap.createBitmap(bitmap, x, y, rectPictureSize.x, rectPictureSize.y);
        int imageWidth = rectBitmap.getWidth();
        int imageHeight = rectBitmap.getHeight();
        FileUtils.saveBitmap(rectBitmap,imagePath);
        setResultUri(imageUri,imageWidth,imageHeight);

        if (bitmap.isRecycled()) {
            bitmap.recycle();
        }
        if (rectBitmap.isRecycled()) {
            rectBitmap.recycle();
        }

        finish();

    }
}
 
Example 8
Project: browser   File: HelpUtils.java   Source Code and License Vote up 5 votes
/**
 *
 * @param options
 * @param data
 * @param path
 * @param uri
 * @param resource
 * @return
 */
public static Bitmap decodeMuilt(BitmapFactory.Options options , byte[] data , String path , Uri uri , int resource) {
    try {

        if(!checkByteArray(data) && TextUtils.isEmpty(path) && uri == null && resource <= 0) {
            return null;
        }

        if(checkByteArray(data)) {
            return BitmapFactory.decodeByteArray(data, 0, data.length);
        }

        if (uri != null){
            InputStream inputStream = MainApp.getContext().getContentResolver().openInputStream(uri);
            Bitmap localBitmap = BitmapFactory.decodeStream(inputStream,null, options);
            inputStream.close();
            return localBitmap;
        }

        if(resource > 0) {
            return BitmapFactory.decodeResource(MainApp.getContext().getResources(), resource);
        }
        return BitmapFactory.decodeFile(path);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 9
Project: FlickLauncher   File: InstallShortcutReceiver.java   Source Code and License Vote up 5 votes
private static PendingInstallShortcutInfo decode(String encoded, Context context) {
    try {
        JSONObject object = (JSONObject) new JSONTokener(encoded).nextValue();
        Intent launcherIntent = Intent.parseUri(object.getString(LAUNCH_INTENT_KEY), 0);

        if (object.optBoolean(APP_SHORTCUT_TYPE_KEY)) {
            // The is an internal launcher target shortcut.
            UserHandleCompat user = UserManagerCompat.getInstance(context)
                    .getUserForSerialNumber(object.getLong(USER_HANDLE_KEY));
            if (user == null) {
                return null;
            }

            LauncherActivityInfoCompat info = LauncherAppsCompat.getInstance(context)
                    .resolveActivity(launcherIntent, user);
            return info == null ? null : new PendingInstallShortcutInfo(info, context);
        }

        Intent data = new Intent();
        data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
        data.putExtra(Intent.EXTRA_SHORTCUT_NAME, object.getString(NAME_KEY));

        String iconBase64 = object.optString(ICON_KEY);
        String iconResourceName = object.optString(ICON_RESOURCE_NAME_KEY);
        String iconResourcePackageName = object.optString(ICON_RESOURCE_PACKAGE_NAME_KEY);
        if (iconBase64 != null && !iconBase64.isEmpty()) {
            byte[] iconArray = Base64.decode(iconBase64, Base64.DEFAULT);
            Bitmap b = BitmapFactory.decodeByteArray(iconArray, 0, iconArray.length);
            data.putExtra(Intent.EXTRA_SHORTCUT_ICON, b);
        } else if (iconResourceName != null && !iconResourceName.isEmpty()) {
            Intent.ShortcutIconResource iconResource =
                new Intent.ShortcutIconResource();
            iconResource.resourceName = iconResourceName;
            iconResource.packageName = iconResourcePackageName;
            data.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        }

        return new PendingInstallShortcutInfo(data, context);
    } catch (JSONException | URISyntaxException e) {
        Log.d(TAG, "Exception reading shortcut to add: " + e);
    }
    return null;
}
 
Example 10
Project: Simpler   File: DrawableUtil.java   Source Code and License Vote up 5 votes
/**
 * byte[]转换成Bitmap
 *
 * @param bytes
 * @return
 */
public static Bitmap bytes2Bitmap(byte[] bytes) {
    if (bytes.length != 0) {
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }
    return null;
}
 
Example 11
Project: SIIEScanner   File: User.java   Source Code and License Vote up 5 votes
public Bitmap createBitMap(Context context) {
    if( this.bitmap == null ){
        this.photoData = loadPhotoData( context );
        if( this.photoData == null ) return null;
        byte [ ] data = Base64.decode( this.photoData, Base64.DEFAULT);
        this.bitmap = BitmapFactory.decodeByteArray( data, 0, data.length);
    }
    return this.bitmap;
}
 
Example 12
Project: Moodr   File: AddMoodActivity.java   Source Code and License Vote up 5 votes
public static Bitmap decodeImage(String imageString) {
    try {
        byte[] encodeByte = Base64.decode(imageString, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
        return bitmap;
    } catch (Exception e) {
        e.getMessage();
        return null;
    }
}
 
Example 13
Project: microMathematics   File: SVGAndroidRenderer.java   Source Code and License Vote up 5 votes
private Bitmap checkForImageDataURL(String url) {
	if (!url.startsWith("data:"))
		return null;
	if (url.length() < 14)
		return null;

	int comma = url.indexOf(',');
	if (comma == -1 || comma < 12)
		return null;
	if (!";base64".equals(url.substring(comma - 7, comma)))
		return null;
	byte[] imageData = Base64.decode(url.substring(comma + 1),
			Base64.DEFAULT);
	return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
}
 
Example 14
Project: LJFramework   File: ImageUtils.java   Source Code and License Vote up 5 votes
/**
 * 按质量压缩
 *
 * @param src 源图片
 * @param quality 质量
 * @param recycle 是否回收
 * @return 质量压缩后的图片
 */
public static Bitmap compressByQuality(Bitmap src, @IntRange(from = 0, to = 100) int quality, boolean recycle) {
    if (isEmptyBitmap(src)) {
        return null;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    src.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    byte[] bytes = baos.toByteArray();
    if (recycle && !src.isRecycled()) {
        src.recycle();
    }
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
 
Example 15
Project: decoy   File: ACache.java   Source Code and License Vote up 4 votes
private static Bitmap Bytes2Bimap(byte[] b) {
	if (b.length == 0) {
		return null;
	}
	return BitmapFactory.decodeByteArray(b, 0, b.length);
}
 
Example 16
Project: GCSApp   File: ACache.java   Source Code and License Vote up 4 votes
private static Bitmap Bytes2Bimap(byte[] b) {
	if (b.length == 0) {
		return null;
	}
	return BitmapFactory.decodeByteArray(b, 0, b.length);
}
 
Example 17
Project: Mp3Player   File: MusicService.java   Source Code and License Vote up 4 votes
public void setCLient()
{
    MediaMetadataRetriever meta = new MediaMetadataRetriever();
    Log.d("current song","of client is "+Current_playing_song.gettitle());
try {
    meta.setDataSource(getBaseContext(), Current_playing_song.getSonguri());
}
catch (Exception e)
{
    e.printStackTrace();
    Toast.makeText(getBaseContext(),"Error in playing song",Toast.LENGTH_SHORT).show();
    playnext();
}
    byte [] b = meta.getEmbeddedPicture();
    Bitmap bitmap;
    if(b!=null)
    {
        bitmap = BitmapFactory.decodeByteArray(b,0,b.length);
    }
    else
    {
        bitmap  = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher,null) ;
    }
    ComponentName receiver = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());
    mediaSession = new MediaSessionCompat(this, "MusicService", receiver, null);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
            .setState(PlaybackStateCompat.STATE_PLAYING, 0, 0)
            .setActions(  PlaybackStateCompat.ACTION_SEEK_TO |
                    PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                    PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                    PlaybackStateCompat.ACTION_PLAY |
                    PlaybackStateCompat.ACTION_PAUSE |
                    PlaybackStateCompat.ACTION_STOP)
            .build());
    mediaSession.setMetadata(new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_ARTIST,Current_playing_song.getartist())
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM,Current_playing_song.getalbum())
            .putString(MediaMetadataCompat.METADATA_KEY_TITLE,Current_playing_song.gettitle())
            .putLong(MediaMetadataCompat.METADATA_KEY_DURATION,Current_playing_song.getDuration())
            .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap)
            .putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bitmap)
            .build());

    mediaSession.setActive(true);
}
 
Example 18
Project: XposedNavigationBar   File: ImageUtil.java   Source Code and License Vote up 4 votes
public static Bitmap byte2Bitmap(byte[] imgBytes) {
    return BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);
}
 
Example 19
Project: GitHub   File: ImageUtils.java   Source Code and License Vote up 2 votes
/**
 * 获取bitmap
 *
 * @param data   数据
 * @param offset 偏移量
 * @return bitmap
 */
public static Bitmap getBitmap(final byte[] data, final int offset) {
    if (data.length == 0) return null;
    return BitmapFactory.decodeByteArray(data, offset, data.length);
}
 
Example 20
Project: RLibrary   File: ImageUtils.java   Source Code and License Vote up 2 votes
/**
 * byteArr转bitmap
 *
 * @param bytes 字节数组
 * @return bitmap
 */
public static Bitmap bytes2Bitmap(byte[] bytes) {
    return (bytes == null || bytes.length == 0) ? null : BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}